Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Unable to use DeleteTrack properly

Discussion in 'Timeline' started by GMF_, Apr 18, 2019.

  1. GMF_


    Apr 2, 2019
    Hi everyone,

    I'm currently working on a script, a part of which allows the user to select an AnimatorClip (as a TimelineClip in an AnimationTrack) he wants to record again, and then creates a "ghost" of that object with a copy of its AnimatorTrack.
    Second track is the one that is copied, fourth one its "ghost" copy

    At the end of the recording process, the "ghost" AnimatorTrack has to be deleted. While destroying the "ghost" GameObject functions properly, I get this result by using the TimelineAsset.DeleteTrack method :

    The thing to notice is the "remaining of the track" on the Timeline. As long as it's here, i get these three errors that gets repeated each time I try to interact with the Timeline :
    First two don't point to any part of the script when double-clicking them, and third one points to the Unity Editor window itself through the Inspector.

    A simple Ctrl+Z removes this "remaining" :
    And another Ctrl+Z is needed to make the track that has been deleted appear again, even though "Edit - Undo" directly makes that track appear again.

    This all seems to be a problem of refreshing the Timeline after the track has been deleted. Any advice to give ?

    Attached Files:

  2. seant_unity


    Unity Technologies

    Aug 25, 2015
    Try using TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved) after deleting the track.
    GeorgeAdamon and GMF_ like this.
  3. GMF_


    Apr 2, 2019
    Hello Sean,
    Thank you for your answer. I initially didn't use that Refresh function as it was a TimelineEditor method, which can only be used in an Editor script ; I ended up solving that problem by using delegates, and using the method you mentionned.

    If anyone encounters the same problem, it might not be the best solution, but here is my workaround. In your MonoBehaviour script, I added the following :

    Code (CSharp):
    1. public delegate void DestroyGhostDelegate();
    2. public static DestroyGhostDelegate OnDestroyGhost;
    4. [...]
    6. private void OnDisable()
    7.     {
    8.             timeline.DeleteTrack(ghostTrack); //timeline is a Timeline Asset, ghostTrack is an AnimationTrack
    10.             if (OnDestroyGhost != null){OnDestroyGhost();}
    11.     }
    In my "Scripts" folder, I added an "Editor" folder, which is required to create Editor script. I added this script in the folder :

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEditor.Timeline;
    4. public class DestroyGhostEditor : Editor
    5. {
    6.     [InitializeOnLoadMethod]
    7.     static void Init()
    8.     {
    9.         InputRecorderTimeline.OnDestroyGhost = RefreshTracks;
    10.     }
    12.     public static void RefreshTracks()
    13.     {
    14.         TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved);
    15.     }
    16. }
    In the end the track gets removed properly with this. Hope it will help someone else !
    seant_unity likes this.
  4. npatch


    Jun 26, 2015
    So this happens only when trying to delete tracks on a Timeline asset on disk during runtime. Correct?
    I tried runtime Timeline asset instances and they didn't have any issues so I no longer need the Timeline asset on disk(all of my content is procedurally generated or loaded on runtime from non compiled assets) and I had no other tracks added before so I had no reason to stick with a disk asset. Though I now wonder whether I could use a disk asset as a template and instantiate the same thing on memory and extend it but destroy it completely at the end.