Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

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

Discussion in 'Timeline' started by Solovykh, Sep 23, 2017.

  1. Solovykh

    Solovykh

    Joined:
    Aug 28, 2017
    Posts:
    59
    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?

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using UnityEngine.Playables;
    4. using UnityEngine.Timeline;
    5.  
    6. public class TimelineExample : EditorWindow
    7. {
    8.     public TimelineClip m_Clip;
    9.     private const float m_FPS = 30.0f;
    10.  
    11.  
    12.     [MenuItem("Window/TimelineExample")]
    13.     public static void ShowWindow()
    14.     {
    15.         GetWindow(typeof(TimelineExample));
    16.     }
    17.  
    18.     void Awake()
    19.     {
    20.         GameObject newObject = new GameObject();
    21.         PlayableDirector playableDirector = newObject.AddComponent<PlayableDirector>();
    22.         TimelineAsset timelineAsset = CreateInstance<TimelineAsset>();
    23.         timelineAsset.editorSettings.fps = m_FPS;
    24.         playableDirector.playableAsset = timelineAsset;
    25.         AssetDatabase.CreateAsset(timelineAsset, "Assets/timelineAsset.playable");
    26.         AnimationTrack track = timelineAsset.CreateTrack<AnimationTrack>(null, "testTrack");
    27.         m_Clip = track.CreateDefaultClip();
    28.     }
    29.  
    30.     void OnGUI()
    31.     {
    32.         int startFrame = (int)(m_Clip.start * m_FPS);
    33.         int newStartFrame;
    34.         if (int.TryParse(EditorGUILayout.TextField("Start Frame:", startFrame.ToString()), out newStartFrame))
    35.         {
    36.             m_Clip.start = newStartFrame / m_FPS;
    37.         }
    38.     }
    39.  
    40.  
    41.  
    42. }
    43.  
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,272
    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().
     
  3. Solovykh

    Solovykh

    Joined:
    Aug 28, 2017
    Posts:
    59
    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.

    Code (CSharp):
    1. var tracks = timelineAsset.GetOutputTracks();
    2. foreach (var track in tracks)
    3. {
    4.        foreach (var timelineClip in track.GetClips())
    5.        {
    6.            if (timelineClip.displayName == m_Clip.displayName) m_Clip = timelineClip;
    7.        }
    8. }
     
unityunity