Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Create timeline assets through script?

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

  1. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    440
    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:
    119
  3. KB73

    KB73

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

    Razouille

    Joined:
    Jan 25, 2013
    Posts:
    39
    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:
    1,281
    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.
  6. kottmann

    kottmann

    Joined:
    Mar 6, 2018
    Posts:
    6
    I do the same as you do, but I have already created a Timeline where I want to add new tracks and clips on runtime. This works great and I can also bind the tracks to the game objects to animate. The tracks are saved correctly. But the bindings are not saved. Does anyone know how to also save the bindings?
     
  7. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,281
    That is a bit trickier. Timeline is an asset, so changes to it will be saved, but the bindings are saved in the playable director, which as part of the scene, gets reverted when play mode is exited.

    The user can do it by copying the values of the playable director in playmode, then pasting them in one in edit mode.There are some solutions to automate that out there, one of which exists in Cinemachine.
     
  8. kottmann

    kottmann

    Joined:
    Mar 6, 2018
    Posts:
    6
    Thanks for your fast reply.
    So I will have a look at SaveDuringPlay in Cinemachine, which I also use in the Timeline anyway.
     
unityunity