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.
  2. Dismiss Notice

Question Adding Animation Track\Clip at Runtime

Discussion in 'Timeline' started by PeachyPixels, Sep 15, 2023.

  1. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    678
    Hello!

    I have a timeline that is part of an in-game tutorial sequence. It has numerous tracks, one of which moves an object from point A (off-screen) to point B (any given point on-screen)

    This is currently achieved by parenting the object in question and animating that over a fixed distance, but that approach has numerous drawbacks.

    I considered moving the object in code whilst also playing the timeline, but this just didn't feel right.

    So I've been experimenting with creating a timeline track dynamically, but am struggling to get it working.

    Currently I have this (excuse the rough code, I'll tidy\refactor after)...

    Code (CSharp):
    1. TimelineAsset asset = <Timeline>.playableAsset as TimelineAsset;
    2.  
    3. TrackAsset existingTrack = <Timeline>.FindTrack("Animation Track (5)");
    4. if (existingTrack != null)
    5.   asset.DeleteTrack(existingTrack);
    6.  
    7. AnimationTrack newTrack = asset.CreateTrack<AnimationTrack>("Animation Track (5)");
    8.  
    9. <Timeline>.SetGenericBinding(newTrack, <TargetGameObject>);
    10.  
    11. TimelineClip clip = newTrack.CreateClip<AnimationPlayableAsset>();
    12. clip.start = 0;
    13. clip.duration = 3;
    14. clip.CreateCurves("Test");
    15.  
    16. AnimationCurve xCurve = new AnimationCurve();
    17. xCurve.AddKey(n, n);
    18. xCurve.AddKey(n, n);
    19. clip.curves.SetCurve(String.Empty, typeof(RectTransform), "m_AnchoredPosition.x", xCurve);
    20.  
    21. AnimationCurve yCurve = new AnimationCurve();
    22. yCurve.AddKey(n, n);
    23. yCurve.AddKey(n, n);
    24. clip.curves.SetCurve(String.Empty, typeof(RectTransform), "m_AnchoredPosition.y", yCurve);
    25.  
    26. <Timeline>.RebuildGraph();
    27.  
    28. <Timeline>.Play();
    It adds the track to the timeline, but does not animate the object. The timeline track shows a yellow warning triangle and the object inspector reports "No animation clip assigned" (even though double-clicking the track opens up the animation with the correct key frames)

    I know it's me doing something silly so any help would be greatly appreciated.
     
  2. tsukimi

    tsukimi

    Joined:
    Dec 10, 2014
    Posts:
    50
    When using AnimationTrack, it's better to use
    AnimationTrack.CreateClip(AnimationClip clip)
    or
    AnimationTrack.CreateInfiniteClip(string infiniteClipName)
    . In your case, it may be CreateInfiniteClip() I think.
    However, is this only an editor thing, or will this also be executed in runtime? The problem is, you can't create an non-legacy animation clip in runtime, after you build the game (the SetCurve() will throw errors).
     
    PeachyPixels likes this.
  3. PeachyPixels

    PeachyPixels

    Joined:
    Feb 17, 2018
    Posts:
    678
    Thanks @tsukimi

    I did some more research over the weekend (based on your feedback) and yes, it seems there are numerous limitations and gotchas with the timeline (and my approach)

    I eventually came across a custom track sample that Unity bundles with timeline, that is doing exactly what's needed...

    https://docs.unity3d.com/Packages/com.unity.timeline@1.6/manual/smpl_custom_tween.html

    https://docs.unity3d.com/Packages/com.unity.timeline@1.8/manual/samp-custom-samples.html

    I imported the sample and managed to get it working with the tutorial timeline, which is good news. But it's made me re-think my entire approach, so may end up re-designing this anyway.

    Thanks again for the info & feedback, it definitely helped.
     
    tsukimi and Yuchen_Chang like this.