Search Unity

Question Animation previsualization in custom timeline track

Discussion in 'Timeline' started by MetroGoldyMayer, Jun 2, 2023.

  1. MetroGoldyMayer

    MetroGoldyMayer

    Joined:
    Sep 5, 2017
    Posts:
    1
    Hello!

    Our project has many dialogue with movement. So, in order to make this cutscenes, we're using Unity's Timeline.

    One of the limitations of the Timeline system is that clips in Animation Tracks are "embedded" in the Timeline's Playable. That means that if you pause the timeline to show a text and wait for the user to press enter to continue, any animations will freeze with the timeline.

    The only way around this I've found is to leave "holes" in the animation tracks. In those frames the Animator component will be freed and it will play its natural animation.

    We've made this behaviour:

    Code (CSharp):
    1.  public class LoopedAnimationBehaviour : PlayableBehaviour
    2.     {
    3.         public  string   ClipName;
    4.         private string   _lastAnimation;
    5.         private Animator _animator;
    6.  
    7.         public override void ProcessFrame(Playable playable, FrameData info, object playerData)
    8.         {
    9.             if (_animator == null)
    10.             {
    11.                 _animator = playerData as Animator;
    12.                 _lastAnimation = _animator.GetCurrentAnimatorClipInfo(0)[0].clip.name;
    13.             }
    14.  
    15.             if (_animator.GetCurrentAnimatorClipInfo(0)[0].clip.name != ClipName) _animator.Play(ClipName);
    16.         }
    17.  
    18.         public override void OnBehaviourPause(Playable playable, FrameData info)
    19.         {
    20.             if (!Application.isPlaying) return;
    21.  
    22.             string actualAnimation = _animator.GetCurrentAnimatorClipInfo(0)[0].clip.name;
    23.             if (actualAnimation == ClipName) _animator.Play(_lastAnimation);
    24.         }
    25.     }
    As soon as the first frame is processed, this behaviour copies the actual clip name and plays the desired clip name. When the clip finishes, if the animator is still playing the assigned clip, it forces the animator to play the last animation in order to restore it.

    This is not very elegant but seems to work just fine for now. The problem is that the other benefit of the Timeline is that it allows you to preview your animations, and, in this case, it won't work.

    We've tried making this PlayableAsset:

    Code (CSharp):
    1.  public class LoopedAnimationAsset : PlayableAsset
    2.     {
    3.         [SerializeField] private AnimationClip _clip;
    4.  
    5.         public override Playable CreatePlayable(PlayableGraph a_graph, GameObject a_owner)
    6.         {
    7.             if (_clip == null) return Playable.Null;
    8.  
    9.             if (!Application.isPlaying) return AnimationClipPlayable.Create(a_graph, _clip);
    10.  
    11.             ScriptPlayable<LoopedAnimationBehaviour> playable =
    12.                 ScriptPlayable<LoopedAnimationBehaviour>.Create(a_graph);
    13.             LoopedAnimationBehaviour behaviour = playable.GetBehaviour();
    14.             behaviour.ClipName = _clip.name;
    15.  
    16.             return playable;
    17.  
    18.         }
    19.     }
    This checks if the application is running. If it is, it returns a Playable with the looped animation behaviour. If not, it returns an AnimationClipPlayable created from the desired clip to make a preview.

    This preview won't be 100% accurate as it doesn't account for the many loops that could happen while the timeline is paused in the actual game, but it's good enough so artist can have an idea of how it will look.

    The LoopedAnimationBehaviour part works fine, but seems like the AnimationClipPlayable is not enough by itself to preview the animation in the Timeline and I'm not sure what to do.

    It probably has to do with the Track and I'll need to make a custom one imitating AnimationTrack, but it is a bit convoluted and I'm still not sure what I need to do.

    Do you have any ideas of how to preview this animations?

    Thank you!