Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Question Trying to get a track to pass a class reference to a playable behavior

Discussion in 'Timeline' started by juicedup, Jul 11, 2022.

  1. juicedup

    juicedup

    Joined:
    Aug 18, 2019
    Posts:
    47
    Hello, I'm a beginner to the playables and timeline API.
    My goal is to make a playable behavior that has a reference to a class on a game object in the scene.
    But the class inherits an interface, I can't pass in an interface
    So I have the track binding as game object and I could use get component and get the reference of the subtype.
    My idea was to use OnCreateClip to pass in the reference to the playable
    https://docs.unity3d.com/2018.3/Documentation/ScriptReference/Timeline.TrackAsset.OnCreateClip.html
    Problem is I don't know how to access the object that the track asset is holding

    process frame has a parameter to the player data which is a reference to the track binding, but using getcomponent here would be expensive.
    https://docs.unity3d.com/2019.4/Doc...Playables.PlayableBehaviour.ProcessFrame.html

    Is there any other way to get access to the track asset's binding object?
     
  2. juicedup

    juicedup

    Joined:
    Aug 18, 2019
    Posts:
    47
    I'm guessing I shouldn't use interfaces?
     
  3. juicedup

    juicedup

    Joined:
    Aug 18, 2019
    Posts:
    47
    upload_2022-7-12_21-58-6.png
    where is group transition stored?
    is it an exposed reference?
     
  4. juicedup

    juicedup

    Joined:
    Aug 18, 2019
    Posts:
    47
  5. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    174
    Let's say we have the following components:

    Code (CSharp):
    1. public class BaseComponent : MonoBehaviour { }
    2. public class DerivedComponent : BaseComponent { }
    I can define a new track like this:

    Code (CSharp):
    1. [TrackBindingType(typeof(BaseComponent))]
    2. public class TrackBindingExample : TrackAsset { }
    The Timeline window will only allow gameobjects with a component that is a BaseComponent (or a derived type) as a binding for your track.

    demo_30.gif

    In ProcessFrame, the playerData object will be the component assigned to the track:

    Code (CSharp):
    1. public class ClipExample : PlayableAsset
    2. {
    3.     public override Playable CreatePlayable(PlayableGraph graph, GameObject owner)
    4.     {
    5.         return ScriptPlayable<PlayableExample>.Create(graph);
    6.     }
    7. }
    8.  
    9. class PlayableExample : PlayableBehaviour
    10. {
    11.     public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    12.     {
    13.         Type dataType = playerData.GetType();
    14.         Debug.Log($"PlayerData type: {dataType}");
    15.     }
    16. }
    After adding the clip to the timeline, the console will print: PlayerData type: DerivedComponent

    If you have a base interface instead of a class, Timeline won't automatically assign the binding; you'll have to add additional code to select the correct component. The TrackEditor class has methods to control which object can be bound to a track: IsBindingAssignableFrom and GetBindingFrom.
     
    Last edited: Jul 19, 2022