Search Unity

Getting Animators from timeline's animation tracks in script

Discussion in 'Timeline' started by Itzhak_P, Oct 20, 2021.

  1. Itzhak_P

    Itzhak_P

    Joined:
    Apr 28, 2015
    Posts:
    12
    I have in my script a ref to my PlayableDirector, and I want to get the animators (i.e 'Button' in the first track in the picture)
    I tried the code below, but it only returns clips for tracks that have them specifically (like the second animation track). For the first track I get nothing, but still want to access the Animator somehow. Any idea how to do that?

    Code (CSharp):
    1. var timelineAsset = myPlayableDirector.playableAsset as TimelineAsset;
    2. foreach (var track in timelineAsset.GetOutputTracks())
    3. {
    4.     var animTrack = track as AnimationTrack;
    5.     if (animTrack != null)
    6.     {
    7.         foreach (var clips in animTrack.GetClips())
    8.         {
    9.  
    10.         }
    11.     }
    12. }
    13.  

    upload_2021-10-20_18-9-29.png
     
  2. grahamsaulnier

    grahamsaulnier

    Unity Technologies

    Joined:
    Apr 10, 2018
    Posts:
    18
    I believe you're looking for
    Code (CSharp):
    1. PlayableDirector.GetGenericBinding
    .

    Code (CSharp):
    1.  
    2.  
    3. TimelineAsset timelineAsset = director.playableAsset as TimelineAsset;
    4. if (timelineAsset != null)
    5. {
    6.     foreach (TrackAsset track in timelineAsset.GetOutputTracks())
    7.     {
    8.         UnityEngine.Object bindingObject = director.GetGenericBinding(track);
    9.         if (bindingObject is Animator animator)
    10.         {
    11.             //...
    12.         }
    13.     }
    14. }
    15.  
    16.