Search Unity

Unable to use DeleteTrack properly

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

  1. GMF_

    GMF_

    Joined:
    Apr 2, 2019
    Posts:
    2
    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.
    upload_2019-4-18_17-20-49.png
    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 :
    upload_2019-4-18_17-15-35.png

    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 :
    upload_2019-4-18_17-21-50.png
    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" :
    upload_2019-4-18_17-17-4.png
    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

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    915
    Try using TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved) after deleting the track.
     
    GMF_ likes this.
  3. GMF_

    GMF_

    Joined:
    Apr 2, 2019
    Posts:
    2
    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;
    3.  
    4. [...]
    5.  
    6. private void OnDisable()
    7.     {
    8.             timeline.DeleteTrack(ghostTrack); //timeline is a Timeline Asset, ghostTrack is an AnimationTrack
    9.  
    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;
    3.  
    4. public class DestroyGhostEditor : Editor
    5. {
    6.     [InitializeOnLoadMethod]
    7.     static void Init()
    8.     {
    9.         InputRecorderTimeline.OnDestroyGhost = RefreshTracks;
    10.     }
    11.  
    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

    npatch

    Joined:
    Jun 26, 2015
    Posts:
    112
    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.