Search Unity

Create timeline assets through script?

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

  1. DGordon

    DGordon

    Joined:
    Dec 8, 2013
    Posts:
    649
    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. gekidoslayer

    gekidoslayer

    Unity Technologies

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

    KB73

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

    Razouille

    Joined:
    Jan 25, 2013
    Posts:
    44
    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,516
    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,516
    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.
     
  9. LorisToia

    LorisToia

    Joined:
    Jan 21, 2021
    Posts:
    14
  10. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,853
    Go to Package Manger window and select the Timeline and Playables package to see the link to proper docs for the package you are using. That link takes me to 2017 docs before they took the API out and made it separate..
     
  11. koumodaki

    koumodaki

    Joined:
    Jun 22, 2021
    Posts:
    9
    I have related issue :- I am making a game which uses timeline sequence video every time new level loads. And for every level we are making assetbundle , now as these timeline sequences uses common animation , in each level, assetbundle for every level is including common animations in it. is there any way that common animations should not go in assetbundle as it increases the size of assetbundle.