Can't edit timeline clip from code after exiting play mode

Hey guys,

I made this simple example to show my problem. I create a timeline clip in code. And I’m able to edit its start time from the editor window just fine. However, as soon as I enter play, and then exit play mode. The link between my editor window start time and timeline clip is broken. What am I doing wrong?

using UnityEditor;
using UnityEngine;
using UnityEngine.Playables;
using UnityEngine.Timeline;

public class TimelineExample : EditorWindow
{
    public TimelineClip m_Clip;
    private const float m_FPS = 30.0f;


    [MenuItem("Window/TimelineExample")]
    public static void ShowWindow()
    {
        GetWindow(typeof(TimelineExample));
    }

    void Awake()
    {
        GameObject newObject = new GameObject();
        PlayableDirector playableDirector = newObject.AddComponent<PlayableDirector>();
        TimelineAsset timelineAsset = CreateInstance<TimelineAsset>();
        timelineAsset.editorSettings.fps = m_FPS;
        playableDirector.playableAsset = timelineAsset;
        AssetDatabase.CreateAsset(timelineAsset, "Assets/timelineAsset.playable");
        AnimationTrack track = timelineAsset.CreateTrack<AnimationTrack>(null, "testTrack");
        m_Clip = track.CreateDefaultClip();
    }

    void OnGUI()
    {
        int startFrame = (int)(m_Clip.start * m_FPS);
        int newStartFrame;
        if (int.TryParse(EditorGUILayout.TextField("Start Frame:", startFrame.ToString()), out newStartFrame))
        {
            m_Clip.start = newStartFrame / m_FPS;
        }
    }



}

TimelineClip does not inherit from UnityEngine.Object, but it is serializable.

So if the window reserializes, it will create a clone of the clip. The same thing will happen if the timeline reloads/reserializes - the clip in your window will no longer be referencing a valid clip.

The simplest solution is probably to reassign your clip in OnEnable().

Hey Sean, thanks for response.

Does this mean that in order to keep track of a timeline clip, I have to do something like this in OnEnable everytime? Seems tedious.

var tracks = timelineAsset.GetOutputTracks();
foreach (var track in tracks)
{
       foreach (var timelineClip in track.GetClips())
       {
           if (timelineClip.displayName == m_Clip.displayName) m_Clip = timelineClip;
       }
}