Timeline assets created through a script gets corrupted?!

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:

string assetPathAndName = AssetDatabase.GenerateUniqueAssetPath($"{nextPath}{infoObject.CharacterName} - {nodeName}.playable");

TimelineAsset newTimelineAsset = ScriptableObject.CreateInstance<TimelineAsset>();

TrackAsset track = newTimelineAsset.CreateTrack<DialogTrack>(null, "Dialog track");

DialogClip dialogClip = ScriptableObject.CreateInstance<DialogClip>();
if (interactableDialogObject.characterData != null)
        dialogClip.characterData = interactableDialogObject.characterData;
dialogClip.dialogTextEntries = new DialogTextEntry[1];
dialogClip.dialogTextEntries[0].PauseBeforeStart = .4f;
dialogClip.dialogTextEntries[0].TypeInterval = .04f;

TimelineClip clip = track.CreateClip<DialogClip>();
clip.asset = dialogClip;
clip.start = 0;
clip.duration = 5;
              
AssetDatabase.CreateAsset(newTimelineAsset, assetPathAndName);

director.playableAsset = newTimelineAsset;

DialogObject dialogObject = GameObject.FindObjectOfType<DialogObject>();
if (dialogObject != null)
{
        director.SetGenericBinding(track, dialogObject);
}

director.time = 3f;

EditorUtility.SetDirty(dialogClip);
EditorUtility.SetDirty(track);
EditorUtility.SetDirty(newTimelineAsset);
AssetDatabase.SaveAssets();
AssetDatabase.Refresh();

I am not getting any errors and there is not much to show either, it’s just an empty timeline asset left.

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)

2 Likes

In case of use, code I use in that area is here: GitHub - alankent/OrdinaryCartoonMaker - it has not crashed for me yet.

UPDATE: Sorry, this is not useful. The specific file I was thinking of was OrdinaryCartoonMaker/Assets/Ordinary Cartoon Maker/Scripts/SequencesApi.cs at main · alankent/OrdinaryCartoonMaker · GitHub, but I just remembered I am using the Unity Sequences package, so I create a new Sequence and that creates the Timeline for me - so it probably does not have useful code after all.

2 Likes

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.

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!

1 Like