Search Unity

Can't select clip or track using C# Selection.activeObject

Discussion in 'Timeline' started by ghtx1138, Feb 26, 2019.

  1. ghtx1138

    ghtx1138

    Joined:
    Dec 11, 2017
    Posts:
    114
    I tried using the code posted by @seant_unity here but I guess some things have changed in the last 2.3 years :)

    The object is selected and there is something there - debug.log prints out "Hello there" but does not continue through the next if condition.

    I'm really after the track object - I'd like to add an animation/custom clip to the selected track. I'm hoping it will be a relatively easy step from selecting the clip to selecting the track.

    Please let me know if I can provide any additional info.

    Code (CSharp):
    1. var obj = UnityEditor.Selection.activeObject;
    2. if (obj != null)
    3. {
    4.     Debug.Log("Hello there"); // this works
    5.  
    6.     var fi = obj.GetType().GetField("m_Item", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
    7.     if (fi != null)
    8.     {
    9.         Debug.Log("You are a bold one");
    10.  
    11.         var clip = fi.GetValue(obj) as TimelineClip;
    12.         if (clip != null)
    13.             Debug.Log(clip.displayName);
    14.  
    15.         //var track = fi.GetValue(obj) as AnimationTrack;
    16.         //if (track != null)
    17.         //    Debug.Log(track.name);
    18.  
    19.     }
    20. }
     
  2. seant_unity

    seant_unity

    Unity Technologies

    Joined:
    Aug 25, 2015
    Posts:
    1,516
    "m_Item" has been changed to "m_Clip".

    In 2019.2 we've added TimelineEditor.selectedClip/selectedClips to make it easier to access.
     
  3. ghtx1138

    ghtx1138

    Joined:
    Dec 11, 2017
    Posts:
    114
    Thanks very much @seant_unity. Works perfectly. I can select a clip in a track and then click a button in the editor to add an curve editable clip. Two click bliss! :D

    For the two click animators of the future (before 2019.2):

    Code (CSharp):
    1. if (GUILayout.Button("Add Clip to Selected Track"))
    2. {
    3.     AnimationTrack theAnimationTrack;
    4.     TimelineClip theAnimationClip;
    5.     AnimationCurve curve;
    6.  
    7.     var obj = UnityEditor.Selection.activeObject;
    8.     if (obj != null)
    9.     {
    10.  
    11.         var fi = obj.GetType().GetField("m_Clip", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy);
    12.         if (fi != null)
    13.         {
    14.  
    15.             var clip = fi.GetValue(obj) as TimelineClip;
    16.             if (clip != null)
    17.                 Debug.Log(clip.displayName);
    18.                 Debug.Log(clip.parentTrack.name);
    19.  
    20.                 theAnimationTrack = clip.parentTrack as AnimationTrack;
    21.  
    22.                 var d = theAnimationTrack.duration;
    23.                 theAnimationClip = theAnimationTrack.CreateRecordableClip("ComingForwardClip");
    24.                 theAnimationClip.start = d;
    25.  
    26.                 theAnimationClip.duration = 20;
    27.                 AnimationPlayableAsset animationPlayableAsset = theAnimationClip.asset as AnimationPlayableAsset;
    28.                 Keyframe[] keys;
    29.                 keys = new Keyframe[3];
    30.                 keys[0] = new Keyframe(0.0f, 2.0f);
    31.                 keys[1] = new Keyframe(1.0f, 0.0f);
    32.                 keys[2] = new Keyframe(2.0f, -1.0f);
    33.                 curve = new AnimationCurve(keys);
    34.  
    35.                 typeof(TimelineClip).GetMethod("AllocateAnimatedParameterCurves", BindingFlags.NonPublic | BindingFlags.Instance).Invoke(theAnimationClip, new object[] { });
    36.                 animationPlayableAsset.clip.SetCurve("", typeof(Transform), "localPosition.z", curve);
    37.  
    38.                 AssetDatabase.SaveAssets();
    39.                 AssetDatabase.Refresh();
    40.         }
    41.     }
    42. }
     
    GMF_ and seant_unity like this.