Search Unity

Resolved Timeline assets created through a script gets corrupted?!

Discussion in 'Timeline' started by NoPanDa, Apr 24, 2023.

  1. NoPanDa

    NoPanDa

    Joined:
    Jan 18, 2020
    Posts:
    10
    So, I have a graph view where I manage and create nodes with Timeline assets and directors assigned to them. It's here very important to be able to create new ones with just the click of button, and I got it to work just as I want to, with one exception: The Timeline assets that I create and save gets corrupted after a while!

    It all seems to be working great for a while, I add some more clips, modify them, save the asset (and the scene), and then suddenly it is corrupted and all of the work that I have done is just gone. The track that I created automatically when creating the timeline asset just disappears, and ofc all the clips with it! I can't for the life of me understand what I am doing wrong, so is this is a bug? I have read all the sources I can find, and I don't think I am doing it wrong... Please help!

    The code I use to create the asset, the track and the first clip:


    Code (CSharp):
    1. string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath($"{nextPath}{infoObject.CharacterName} - {nodeName}.playable");
    2.  
    3. TimelineAsset newTimelineAsset = ScriptableObject.CreateInstance<TimelineAsset>();
    4.  
    5. TrackAsset track = newTimelineAsset.CreateTrack<DialogTrack>(null, "Dialog track");
    6.  
    7. DialogClip dialogClip = ScriptableObject.CreateInstance<DialogClip>();
    8. if (interactableDialogObject.characterData != null)
    9.         dialogClip.characterData = interactableDialogObject.characterData;
    10. dialogClip.dialogTextEntries = new DialogTextEntry[1];
    11. dialogClip.dialogTextEntries[0].PauseBeforeStart = .4f;
    12. dialogClip.dialogTextEntries[0].TypeInterval = .04f;
    13.  
    14. TimelineClip clip = track.CreateClip<DialogClip>();
    15. clip.asset = dialogClip;
    16. clip.start = 0;
    17. clip.duration = 5;
    18.              
    19. AssetDatabase.CreateAsset(newTimelineAsset, assetPathAndName);
    20.  
    21. director.playableAsset = newTimelineAsset;
    22.  
    23. DialogObject dialogObject = GameObject.FindObjectOfType<DialogObject>();
    24. if (dialogObject != null)
    25. {
    26.         director.SetGenericBinding(track, dialogObject);
    27. }
    28.  
    29. director.time = 3f;
    30.  
    31. EditorUtility.SetDirty(dialogClip);
    32. EditorUtility.SetDirty(track);
    33. EditorUtility.SetDirty(newTimelineAsset);
    34. AssetDatabase.SaveAssets();
    35. AssetDatabase.Refresh();
    I am not getting any errors and there is not much to show either, it's just an empty timeline asset left.
     
  2. tsukimi

    tsukimi

    Joined:
    Dec 10, 2014
    Posts:
    80
    tracks and clips are saved as sub-asset of the timelineAsset, so maybe call
    AssetDatabase.CreateAsset()
    right after
    CreateInstance()
    (and maybe load again) will help. (at line 5-7, the TimelineAsset doesn't exist in project when creating tracks and clips, so the info of tracks and clips are lost, but I'm not sure)
     
    NoPanDa and akent99 like this.
  3. akent99

    akent99

    Joined:
    Jan 14, 2018
    Posts:
    588
    Last edited: Apr 24, 2023
    NoPanDa and tsukimi like this.
  4. NoPanDa

    NoPanDa

    Joined:
    Jan 18, 2020
    Posts:
    10
    Thank you both!

    I'll try creating the asset earlier, and then look into the Sequences solution if nothing else helps >.<

    I'll report back asap.
     
  5. NoPanDa

    NoPanDa

    Joined:
    Jan 18, 2020
    Posts:
    10
    Ok, I solved it. I was doing it wrong xD

    I either need to save the Clip asset in the asset database as a separate object, or I need to create the clip through the track, like I do now, and then cast the already created clip-asset to a DialogClip, and set the values that way.

    It's kind of obvious now when I think about it xD

    Thanks for your suggestions though!
     
    tsukimi likes this.