Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Is each PlayableBehaviour bound to a single ScriptPlayable (and vice versa)?

Discussion in 'Timeline' started by LazloBonin, Oct 29, 2020.

  1. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    818
    I'm wondering if we can use our implementation of PlayableBehaviour to store per-playable/per-clip data.

    When we create a script playable, we do something like:

    Code (CSharp):
    1.         public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    2.         {
    3.             var playable = ScriptPlayable<MyBehaviour>.Create(graph);
    4.             var behaviour = playable.GetBehaviour();
    5.             return playable;
    6.         }
    If we were to pass the Playable reference back to the Behaviour, would that be guaranteed to be a 1-to-1 mapping? In other words, can we rely on the fact that each behaviour instance is mapped to a single playable, and vice versa, is each playable governed by a single behaviour?

    Concretely, am I shooting myself in the foot by doing this:

    Code (CSharp):
    1.         public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    2.         {
    3.             var playable = ScriptPlayable<MyBehaviour>.Create(graph);
    4.             var behaviour = playable.GetBehaviour();
    5.             behaviour.playable = playable;
    6.             return playable;
    7.         }
    Or in other words, will this return true, for example:

    Code (CSharp):
    1.         public class MyBehaviour : PlayableBehaviour
    2. {
    3.     public Playable playable;
    4.  
    5.     void OnProcessFrame(Playable playable, /* etc. */)
    6.     {
    7.         Debug.Log(playable == this.playable);
    8.     }
    9. }
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    Yes - that is a reasonable assumption to make. A script playable is simply a playable with a behaviour attached, and they are always a pair.
     
    LazloBonin likes this.
  3. LazloBonin

    LazloBonin

    Joined:
    Mar 6, 2015
    Posts:
    818
    Thanks for the quick confirmation! :)