Search Unity

Question Extended Timeline: Get clips from binding object

Discussion in 'Timeline' started by Tulsisvt, Jan 13, 2021.

  1. Tulsisvt

    Tulsisvt

    Joined:
    May 5, 2015
    Posts:
    28
    I have a custom track (TrackAsset) with a binding type that is a monobehaviour script (lets call it ClipController). I want to get a list of clips in that track and access that from ClipController. I know I can use GetClips() in TrackAsset. But, is there a way I can do that from the ClipController. or access the track asset from ClipController and call GetClips() through reference.
     
  2. DavidGeoffroy

    DavidGeoffroy

    Unity Technologies

    Joined:
    Sep 9, 2014
    Posts:
    542
    Copy-pasted from my UnityAnswers answer:
    No, you need to know what you're looking for. The MonoBehaviour has no idea that anything is bound to it.

    You need to know which PlayableDirector owns the Timeline, then you need to go through every track and ask the PlayableDirector what the object bound to that track is and compare it to your MonoBehaviour. Once you have found which track is bound to your MonoBehaviour, then you can ask the track what its clips are.

    The pseudocode for a function that finds the clips associated with a monobehaviour looks something like this:
    Code (CSharp):
    1. for every PlayableDirector Component in the scene
    2.    If it has a Playable asset and that PlayableAsset is a Timeline
    3.      For every Track in the Timeline
    4.        If the associated binding in the PlayableDirector is my MonoBehaviour
    5.          Add all clips from the track to the list of clips
    6. Return the list  of clips
    There can be more than one track bound to a component, from more than one PlayableDirector, so depending on what you're trying to do, you may want to add more logic.
     
    Tulsisvt likes this.
  3. Tulsisvt

    Tulsisvt

    Joined:
    May 5, 2015
    Posts:
    28
    Thank you so much! This works for me. Although I would need to get the track that uses this monobehaviour instance as its binding type. How can i get that?

    Code (CSharp):
    1.  foreach (var director in GameObject.FindObjectsOfType<PlayableDirector>())
    2.             {
    3.                 if (director.playableAsset.GetType() == typeof(TimelineAsset))
    4.                 {
    5.                     var timelineAsset = director.playableAsset;
    6.  
    7.                     foreach (var track in timelineAsset.outputs)
    8.                     {
    9.                         if ((MyTrack) track.sourceObject)
    10.                         {
    11.                             var mytrack= (MyTrack) track.sourceObject;
    12.                             foreach (var c in mytrack.GetClips())
    13.                             {
    14.                                 Debug.Log(c.displayName);
    15.                             }
    16.                         }
    17.                     }
    18.                 }
    19.             }
    Edit: Ok I used to get the current binding. Thanks for your help!
    Code (CSharp):
    1. director.GetGenericBinding(myTrack)
     
    Last edited: Jan 14, 2021