Search Unity

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

Discussion in 'Timeline' started by samuelmorais, Dec 5, 2019.

  1. samuelmorais

    samuelmorais

    Joined:
    Aug 3, 2012
    Posts:
    62
    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:



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.Timeline;
    5. using UnityEngine.Playables;
    6. using UnityEngine.UI;
    7.  
    8. public class TestsTimeline : MonoBehaviour
    9. {
    10.     PlayableDirector director;
    11.     TimelineAsset timelineAsset;
    12.     public AnimationClip clip1;
    13.     public AnimationClip clip2;
    14.     public Slider sliderTimeline;
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         director = GetComponent<PlayableDirector>();
    19.         timelineAsset = (TimelineAsset)director.playableAsset;      
    20.     }
    21.  
    22.  
    23.     public void AddAnimationClip()
    24.     {
    25.         var newTrack = (AnimationTrack)timelineAsset.GetRootTrack(0);
    26.  
    27.         director.SetGenericBinding(newTrack, gameObject);
    28.  
    29.         var timelineClip = newTrack.CreateClip(clip1);
    30.  
    31.         var timelineClip2 = newTrack.CreateClip(clip2);
    32.  
    33.         director.RebuildGraph();
    34.     }
    35.  
    36.    
    37.  
    38.     public void OnChangeTimeline()
    39.     {
    40.         var value = sliderTimeline.value;
    41.         director.time = value;
    42.         director.RebuildGraph();
    43.         director.Evaluate();
    44.        
    45.     }
    46. }
    47.  
     
  2. samuelmorais

    samuelmorais

    Joined:
    Aug 3, 2012
    Posts:
    62
    Just to clarify, my question is about how to move the slider and see the animations playing, like they play in edit mode. Thanks!
     
  3. elfasito

    elfasito

    Joined:
    Jul 4, 2017
    Posts:
    51
    Hello Samuel.
    you finally resolved this?, I want to do the same, but without lucky for now
     
  4. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    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 - https://docs.unity3d.com/2019.1/Documentation/ScriptReference/Animations.GameObjectRecorder.html

    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.
     
    samuelmorais likes this.