Search Unity

Question How to find/reveal/focus track in Timeline window?

Discussion in 'Timeline' started by Peter77, Dec 22, 2022.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,609
    I'm looking for a way to find/reveal/focus/highlight a track with a specific generic binding in the Timeline window. The idea is that you can drag&drop a GameObject from the Hierarchy to a "Find in Timeline" window, which will then show the track in the Timeline window.

    Drag&Drop "Crawler_Timeline" from Hierarchy to "Find in Timeline" window:

    upload_2022-12-22_20-45-4.png

    The Timeline window should then reveal the first track that has the object as its generic binding. In this case, expand all groups and scroll the view to the track:

    upload_2022-12-22_20-46-28.png

    I'm not looking for a textbook solution, I really just need this functionality. Hacks and reflection are perfectly fine for me. The reason for this requirement is that the missing search functionality in Timeline is currently killing our development times, because our timeline asset is rather complex and it's hard to find things.

    Here is the code I came up with so far. It's able to find the track, but it's missing how to show the track in the Timeline window.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEditor;
    5. using UnityEditor.Timeline;
    6. using UnityEngine.Timeline;
    7.  
    8.     class FindInTimeline : EditorWindow
    9.     {
    10.         Object m_Obj;
    11.  
    12.         [MenuItem("Window/Sequencing/Find in Timeline", priority = 300)]
    13.         static void DoMenuItem()
    14.         {
    15.             var wnd = EditorWindow.GetWindow<FindInTimeline>();
    16.             wnd.titleContent = new GUIContent($"Find in Timeline");
    17.             wnd.Show();
    18.         }
    19.  
    20.         void OnGUI()
    21.         {
    22.             var newObj = EditorGUILayout.ObjectField("Find", m_Obj, typeof(Object), true);
    23.             if (newObj != m_Obj)
    24.             {
    25.                 m_Obj = newObj;
    26.                 Find(m_Obj);
    27.             }
    28.         }
    29.  
    30.         void Find(Object obj)
    31.         {
    32.             var director = TimelineEditor.inspectedDirector;
    33.             var timeline = TimelineEditor.inspectedAsset;
    34.  
    35.             foreach (var track in timeline.GetOutputTracks())
    36.             {
    37.                 if (director.GetGenericBinding(track) != obj)
    38.                     continue;
    39.  
    40.                 // now i want to reveal the current track in the Timeline window
    41.                 // and focus/highlight it
    42.                 Debug.Log(track.name);
    43.             }
    44.         }
    45.     }
    46.  
     
  2. julienb

    julienb

    Unity Technologies

    Joined:
    Sep 9, 2016
    Posts:
    177
    You can use this method to reveal a track. Each group track that is part of a track's hierarchy will be opened. This can be done using the public api:
    Code (CSharp):
    1. static void Reveal(TrackAsset track)
    2. {
    3.     while (track != null)
    4.     {
    5.         track.SetCollapsed(false);
    6.         track = track.parent as TrackAsset;
    7.     }
    8.     TimelineEditor.Refresh(RefreshReason.ContentsAddedOrRemoved);
    9. }
    To scroll to a track, there is no public API to do this. There is an internal method that can do this though:

    Code (CSharp):
    1. static void ScrollTo(TrackAsset track)
    2. {
    3.     Selection.activeObject = track;
    4.     InvokeInternalFrameTrack();
    5. }
    6.  
    7. static void InvokeInternalFrameTrack()
    8. {
    9.     const string keyboardNavigationTypeName = "UnityEditor.Timeline.KeyboardNavigation";
    10.     const string frameTrackHeaderMethodName = "FrameTrackHeader";
    11.    
    12.     Assembly timelineEditorAssembly = typeof(TimelineEditorWindow).Assembly;
    13.     Type keyboardNavigation = timelineEditorAssembly.GetType(keyboardNavigationTypeName);
    14.     MethodInfo method = keyboardNavigation.GetMethod(frameTrackHeaderMethodName);
    15.     method.Invoke(null, new[] { Type.Missing });
    16. }
    Please note that there is no guarantee of support or backwards compatibility in future versions for internal/private methods.
     
    Peter77 likes this.