Search Unity

  1. Unity 2019.2 is now released.
    Dismiss Notice

Accessing properties of the currently selected Clip in the Timeline Window

Discussion in 'Timeline' started by theronlad, Dec 18, 2017.

  1. theronlad

    theronlad

    Joined:
    Nov 27, 2013
    Posts:
    3
    Hi,

    I'm looking for a way to access properties on the clip that is currently highlighted in the Timeline Window from code. Basically I am using nested timelines by having clips in a control track that refer to other game objects that have a timeline on them. I'd like to add some automation for which I need to find out which game object the currently selected clip is pointing to.

    As the inspector window is showing properties of the currently selected clip I have tried going down that route. Using Selection.activeObject I can get the currently selected object and I can see that is of type UnityEditor.Timeline.EditorClip. When I tried to cast the object to that type in an attempt to get some info from it though I can't as it is inaccessible due to its protection level.

    Does anyone have a suggestion on how I could access this information?
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    909
    From the editor clip class, you can use reflection to get the m_Item member, which is of type TimelineClip which is in the public API.
     
  3. theronlad

    theronlad

    Joined:
    Nov 27, 2013
    Posts:
    3
    Thanks for your reply seant.

    Would it be possible to be a bit more specific, i.e. if I use [ var obj = Selection.activeObject ] to get obj which is of type EditorClip, how do I get from there to the TimelineClip? I have used reflection to list all the members of obj but the m_Item you mentioned is not one of them?
     
  4. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    909
    Code (CSharp):
    1.         var obj = UnityEditor.Selection.activeObject;
    2.         if (obj != null)
    3.         {
    4.             var fi = obj.GetType().GetField("m_Item", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy );
    5.             if (fi != null)
    6.             {
    7.                 var clip = fi.GetValue(obj) as TimelineClip;
    8.                 if (clip != null)
    9.                     Debug.Log(clip.displayName);
    10.             }
    11.         }
    12.  
    I hope that helps!
     
  5. theronlad

    theronlad

    Joined:
    Nov 27, 2013
    Posts:
    3
    That absolutely helps, thanks a lot!

    (I was using .GetType().GetMembers() without arguments)