How to create a slider to scrub timeline asset at runtime?

Hi,

I want to create an animation system in Unity.

It is a character animation system with an interface like maya, 3d studio, blender etc.

The idea is to move the characters and record its positions and animations over time.

And my question is: how to do this with Unity timeline?

Is it possible to create clips at runtime and play them?

I tried to do so, but the animation was not playing.

Here is the script that I am calling on Slider Change:

Here is the video:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Playables;
using UnityEngine.UI;

public class TestsTimeline : MonoBehaviour
{
    PlayableDirector director;
    TimelineAsset timelineAsset;
    public AnimationClip clip1;
    public AnimationClip clip2;
    public Slider sliderTimeline;
    // Start is called before the first frame update
    void Start()
    {
        director = GetComponent<PlayableDirector>();
        timelineAsset = (TimelineAsset)director.playableAsset;      
    }


    public void AddAnimationClip()
    {
        var newTrack = (AnimationTrack)timelineAsset.GetRootTrack(0);

        director.SetGenericBinding(newTrack, gameObject);

        var timelineClip = newTrack.CreateClip(clip1);

        var timelineClip2 = newTrack.CreateClip(clip2);

        director.RebuildGraph();
    }

   

    public void OnChangeTimeline()
    {
        var value = sliderTimeline.value;
        director.time = value;
        director.RebuildGraph();
        director.Evaluate();
       
    }
}

Just to clarify, my question is about how to move the slider and see the animations playing, like they play in edit mode. Thanks!

Hello Samuel.
you finally resolved this?, I want to do the same, but without lucky for now

It isn’t possible to create animation clips at runtime - at least not in a player. AnimationClips compatible with runtime playables need to be created using UnityEditor APIs. If you are ok with that restriction, check out the gameObject recorder - Unity - Scripting API: GameObjectRecorder

The rest of your script looks fine, timelines can be created and scrubbed at runtime. You can also just use AnimationPlayables for more precises control.

1 Like