Search Unity

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:
    1,516
    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:
    1,516
    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)
     
  6. DeloitteCam

    DeloitteCam

    Joined:
    Jan 23, 2018
    Posts:
    22
    In reference to the above, "m_Item" is now "m_Clip".

    Can unity please expose this type so that we don't have to have self destructive dependencies in our tool code.
     
  7. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    kimyir2 and Euricius like this.