Search Unity

How to select a timeline clip by scirpts?

Discussion in 'Timeline' started by Direwolf33, Nov 30, 2018.

  1. Direwolf33

    Direwolf33

    Joined:
    Dec 15, 2016
    Posts:
    3
    I wanted to develop some editor functions for timeline.But I found i cant use TimelineClip to fill in Selection.objects, and I found it must be EditorClip, but I dont know how to get this object, is there anyone know that?
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
  3. Direwolf33

    Direwolf33

    Joined:
    Dec 15, 2016
    Posts:
    3
  4. Direwolf33

    Direwolf33

    Joined:
    Dec 15, 2016
    Posts:
    3
    Hi, I tried the way that you said.And I can show the clips in Inspector window.But i cant make the clips into selected state (highlingt) in Timeline Window. Is there any way to make the clips look like focused just i select them by using mouse?
     
    Last edited: Dec 9, 2018
  5. turbolek

    turbolek

    Joined:
    Dec 30, 2016
    Posts:
    11
    Hi, i know this topic is old, but I had the exact same problem and this was still most recent and best match topic, so possibly other people facing this issue will find my findings helpful.

    I didn't find a legit way to highlight selected clip on a timeline, but I found a workaround that results with the desired effect.
    Basically, turns out that it is possible to navigate throught Timeline Window content with arrow keys on your keyboard and it is possible to emulate these keyboard events through code.
    My approach is to find a specific track and group that my clip belongs to, then select the first (top most) track in the timeline asset and then navigate to target track, target group and finally target clip.

    Here's the code:

    Code (CSharp):
    1. private void SelectClip(TimelineClip myClip)
    2.     {
    3.         timelineWindow = EditorWindow.GetWindow<TimelineEditorWindow>();
    4.         timelineWindow.Focus();
    5.                
    6.         var targetTrack = myClip.GetParentTrack();
    7.         var targetGroup = targetTrack.GetGroup();
    8.         var timelineAsset = targetTrack.timelineAsset;
    9.        
    10.         var firstTrack = timelineAsset.GetOutputTrack(0);
    11.         Selection.activeObject = firstTrack;
    12.  
    13.         if (targetGroup != null)
    14.         {
    15.             //Select group
    16.             while (Selection.activeObject != targetGroup)
    17.             {
    18.                 var activeObject = Selection.activeObject;
    19.                 timelineWindow.SendEvent(Event.KeyboardEvent("down"));
    20.                 if (Selection.activeObject == activeObject)
    21.                 {
    22.                     Debug.LogError("Could not find proper track under Warning Track");
    23.                     return;
    24.                 }
    25.             }
    26.  
    27.             //Expand group if collapsed
    28.             if (targetGroup.IsCollapsed())
    29.             {
    30.                 timelineWindow.SendEvent(Event.KeyboardEvent("right"));
    31.             }
    32.         }
    33.  
    34.         while (Selection.activeObject != targetTrack)
    35.         {
    36.             //Select track
    37.             timelineWindow.SendEvent(Event.KeyboardEvent("down"));
    38.         }
    39.  
    40.  
    41.         var trackClips = targetTrack.GetClips().ToList();
    42.         int clipIndex = trackClips.FindIndex(c => Math.Abs(c.start - myClip.start) < 0.01 && c.displayName == myClip.displayName);
    43.  
    44.         for (int j = 0; j <= clipIndex; j++)
    45.         {
    46.             timelineWindow.SendEvent(Event.KeyboardEvent("right"));
    47.         }
    48.        
    49.         timelineWindow.Repaint();
    50.     }
     
    AnomalusUndrdog likes this.
  6. AnomalusUndrdog

    AnomalusUndrdog

    Joined:
    Jul 3, 2009
    Posts:
    1,553
    @turbolek Thanks for the code! But
    timelineAsset.GetOutputTrack(0)
    was returning the wrong track for me (when there's a group at the topmost part of the timeline, that group gets skipped. What worked for me was replacing that with
    timelineAsset.GetRootTrack(0)
    .