Search Unity

ScriptPlayable - shared stateless behaviour

Discussion in 'Animation' started by OndrejP, Feb 7, 2020.

  1. OndrejP

    OndrejP

    Joined:
    Jul 19, 2017
    Posts:
    304
    When ScriptPlayable is created, it either creates new instance of PlayableBehaviour or clones existing from template argument.

    There are some cases when my behaviour are stateless and I'd be okay sharing single instance with multiple ScriptPlayables. Is it safe to override PlayableBehaviour.Clone to return this?

    This way I could create shared instance and reuse it in multiple calls of:
    ScriptPlayable<T>.Create(graph, mySharedInstance, 1)
     
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    Try it and see. It seems unlikely that anyone else would have tried that and there is practically zero chance of the documentation accounting for it.

    But if it's stateless then having a bunch of extra instances in memory would cost practically nothing compared to the rest of the graph.
     
  3. OndrejP

    OndrejP

    Joined:
    Jul 19, 2017
    Posts:
    304
    So far it seems to work fine, no crashes, behaves as expected.
    It has no state, but there's some small immutable "configuration data".