Programmatically Creating a Timeline
You can do this via creating TimelineAssets, TrackAssets, and finally TimelineClips via TimelineAsset’s create API.
var tracks = timelineToUpdate.GetRootTracks();
DialogueTrack targetTrack = null;
foreach (TrackAsset track in tracks) {
Debug.Log("Currently printing track " + track.name);
Debug.Log(track.GetType());
if (track.name == "Dialogue") {
Debug.Log("Removing existing dialogue track");
foreach (TimelineClip clip in track.GetClips()) {
timelineToUpdate.DeleteClip(clip);
}
targetTrack = (DialogueTrack)track;
}
}
if (!targetTrack) {
targetTrack = timelineToUpdate.CreateTrack<DialogueTrack>(null, "Dialogue");
}
...
// this section of code creates a new clip on the timeline track
// given some base parameters.
TimelineClip clip = targetTrack.CreateClip<DialogueClip>();
DialogueClip dialogueClip = (DialogueClip)clip.asset;
clip.start = timestamp;
clip.duration = action == "hide" ? 1f : 5f;
// these are public variables on the Behavior attached to the Clip.
dialogueClip.template.character = character;
dialogueClip.template.shouldHideDialogueBox = action == "hide" ? true : false;
dialogueClip.template.dialogueText = dialogueText;
dialogueClip.template.x = x;
dialogueClip.template.y = y;
Note that when you programmatically create a timeline like this, the timeline UI in the editor won’t update until you toggle between different timelines! Be sure to refresh it to avoid looking at invalid clips.
API https://docs.unity3d.com/2018.3/Documentation/ScriptReference/Timeline.TimelineClip.html
References: https://forum.unity.com/threads/create-timeline-assets-through-script.493331/ https://forum.unity.com/threads/deleting-timelineclip-at-runtime.498006/
Getting a specific type of a clip from a TimelineClip: https://forum.unity.com/threads/how-to-get-the-timelineclip-for-a-playablebehaviour.503413/#post-3280659