Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Create timeline assets through script?

Discussion in 'Timeline' started by DGordon, Sep 5, 2017.

  1. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    396
    Is it possible to generate a new timeline and populate it with tracks/behaviors through script?

    The use-case which is driving this question is we have about 50 short timelines we are creating. They have 1 looping animation, 1 track for blinking (custom), and 1 track for lip sync data (custom extension on Rogo LipSync I put together).

    It would be fantastic if we could batch process these in a script which loops through all the assets and assembles each timeline. The ability to use code/editor tooling to create these would be very powerful.
     
  2. mikew_unity

    mikew_unity

    Unity Technologies

    Joined:
    Sep 27, 2016
    Posts:
    97
  3. KB73

    KB73

    Joined:
    Feb 7, 2013
    Posts:
    169
  4. Razouille

    Razouille

    Joined:
    Jan 25, 2013
    Posts:
    37
    Hellow,
    I'm trying to generate timelines through script to avoid some user interactions on the scene.
    The generation is working well, the created timeline has the same look as when I create it manually, and the preview works fine, but when I hit play, all my generated tracks disappear in the timeline and nothing is played after (but the length is kept, check the screenshot : ).
    Here is a part of my code :
    Code (CSharp):
    1. GameObject directorGO = new GameObject("Timeline");
    2. directorGO.AddComponent<CutsceneDirector>();
    3. PlayableDirector director = directorGO.AddComponent<PlayableDirector>();
    4. PlayableGraph graph = PlayableGraph.Create();
    5.  
    6. TimelineAsset timeline = new TimelineAsset();
    7. AnimationTrack track = null;
    8. for (int i = 0; i < clips.Count; ++i)
    9. {
    10.     AnimationClip clip = ImportMotion(clips[i]);
    11.     AnimationPlayableOutput output = AnimationPlayableOutput.Create(graph, clip.name, model.GetComponent<Animator>());
    12.     AnimationClipPlayable playable = AnimationClipPlayable.Create(graph, clip);
    13.     output.SetSourcePlayable<AnimationPlayableOutput, AnimationClipPlayable>(playable);
    14.  
    15.     TimelineClip timelineClip = track.CreateClip(clip);
    16.     timelineClip.duration = clip.averageDuration;
    17.     timelineClip.displayName = clip.name;
    18.  
    19.     director.SetGenericBinding(track, model);
    20. }
    21.  
    22. AssetDatabase.CreateAsset(timeline, timelinesFolder + "/Timeline.playable");
    23. director.playableAsset = timeline;
    24.  
    Do you have any idea ? Actually I don't understand how to use the "PlayableGraph.Create()" method, maybe this is where my problem is ?

    EDIT : the only difference in the view is when I click on a Track, I don't have "Asset Labels" displayed in the Inspector window, at the bottom.

    EDIT 2 : found the solution, actually the timeline save asset must be done first, or the modifications are not saved, like if I were on Play mode
     
    Last edited: Dec 15, 2017
  5. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    909
    Exactly. The tracks and clips are (hidden) sub assets of the timeline asset. The timeline needs to exist in the asset database before the tracks are added.
     
    Razouille likes this.