Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Access the animation clip I created in a playable through code

Discussion in 'Timeline' started by Bill-Sansky, Aug 8, 2017.

  1. Bill-Sansky

    Bill-Sansky

    Joined:
    Oct 2, 2016
    Posts:
    59
    Is there a way to access the animation clips created in the playable editor through the playable API?

    I created a playable that I play with a playable director, but I can't seem to find a way to access dynamically the animation clips the playable contains through code.

    Is there any way to do that?
     
  2. Bill-Sansky

    Bill-Sansky

    Joined:
    Oct 2, 2016
    Posts:
    59
  3. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    909
    Do you mean from a timeline?
     
  4. Bill-Sansky

    Bill-Sansky

    Joined:
    Oct 2, 2016
    Posts:
    59
    Yes I suppose: I mean that I have a playable director attached to a game object, I created a playable with a timeline and I start it with the director in a script.
     
  5. Bill-Sansky

    Bill-Sansky

    Joined:
    Oct 2, 2016
    Posts:
    59
    In fact, I could extend my question to any properties modified in a playable: Is there any way to access to data of the key frames inside a playable asset, to get and set the values used by a key frame of a specific animation?
     
  6. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    909
    If you convert your animation tracks to clip tracks, you can access the animation clips through the timeline API. You can edit the clips using the AnimationUtility class, just like any other clip.

    Code (CSharp):
    1. var timelineAsset = playableDirector.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.             var animAsset = clips.asset as AnimationPlayableAsset;
    10.             if (animAsset)
    11.             {
    12.                 AnimationClip clip = animAsset.clip;
    13.                 ...
    14.            
    15.             }
    16.        
    17.         }      
    18.      }
    19. }
    20.  
    If you want the keyframes from a generic playable animation, that is stored in TimelineClip.curves.
     
    fastgamedev likes this.
  7. Bill-Sansky

    Bill-Sansky

    Joined:
    Oct 2, 2016
    Posts:
    59
    Got it, I did not pay attention to the fact that I could cast my tracks to animation tracks.
    That is super useful, thanks a lot!