Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Getting current active animation clip from the animation editor window?

Discussion in 'Scripting' started by petarnordeus, Jan 22, 2020.

  1. petarnordeus

    petarnordeus

    Joined:
    Aug 4, 2017
    Posts:
    7
    So i want to get the active animation clip from the animation editor:
    upload_2020-1-22_12-16-49.png

    I found some help on another post but its not working, and I cannot figure out whats wrong. Unity returns:

    MissingMethodException: UnityEditorInternal.AnimationWindowState.get_activeAnimationClip Due to: Attempted to access a missing member.

    but when I try to print all the methods, the getter for ActiveAnimationClip is there! I even checked in UnityCsReference/blob/master/Editor/Mono/Animation/AnimationWindow/AnimationWindowState.cs
    upload_2020-1-22_12-26-27.png


    Here is the code that I used for this:

    Code (CSharp):
    1. using System;
    2. using System.Reflection;
    3. using UnityEditor;
    4. using UnityEngine;
    5. using Object = System.Object;
    6.  
    7. public class wAnimationWindowHelper
    8. {
    9.     [MenuItem("CustomStuff/PrintActiveClip")]
    10.     public static void GetActiveClip()
    11.     {
    12.         GetAnimationWindowCurrentClip();
    13.     }
    14.  
    15.  
    16.     static System.Type animationWindowType = null;
    17.  
    18.     static System.Type GetAnimationWindowType()
    19.     {
    20.         if (animationWindowType == null)
    21.         {
    22.             animationWindowType = System.Type.GetType("UnityEditor.AnimationWindow,UnityEditor");
    23.         }
    24.  
    25.         return animationWindowType;
    26.     }
    27.  
    28.     static UnityEngine.Object GetOpenAnimationWindow()
    29.     {
    30.         UnityEngine.Object[] openAnimationWindows = Resources.FindObjectsOfTypeAll(GetAnimationWindowType());
    31.         if (openAnimationWindows.Length > 0)
    32.         {
    33.             return openAnimationWindows[0];
    34.         }
    35.  
    36.         return null;
    37.     }
    38.  
    39.  
    40.     static AnimationClip GetAnimationWindowCurrentClip()
    41.     {
    42.         UnityEngine.Object w = GetOpenAnimationWindow();
    43.         if (w != null)
    44.         {
    45.             BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
    46.             FieldInfo animEditor = GetAnimationWindowType().GetField("m_AnimEditor", flags);
    47.  
    48.             Type animEditorType = animEditor.FieldType;
    49.             System.Object animEditorObject = animEditor.GetValue(w);
    50.             FieldInfo animWindowState = animEditorType.GetField("m_State", flags);
    51.             Type windowStateType = animWindowState.FieldType;
    52.  
    53.             System.Object clip = windowStateType.InvokeMember("get_activeAnimationClip",
    54.                 BindingFlags.InvokeMethod | BindingFlags.Public, null, animWindowState.GetValue(animEditorObject),
    55.                 null);
    56.  
    57.             Debug.Log(clip);
    58.            
    59.  
    60.             return (AnimationClip) clip;
    61.         }
    62.  
    63.         return null;
    64.     }
    65. }
     
  2. vladibalan

    vladibalan

    Joined:
    Sep 14, 2014
    Posts:
    6
    If you haven't found a solution yet, this worked for me:
    I replaced
    Code (CSharp):
    1. System.Object clip = windowStateType.InvokeMember("get_activeAnimationClip", BindingFlags.InvokeMethod | BindingFlags.Public, null, animWindowState.GetValue(animEditorObject), null);
    with
    Code (CSharp):
    1. PropertyInfo propInfo = windowStateType.GetProperty("activeAnimationClip");
    2. System.Object clip = propInfo.GetValue(animWindowState.GetValue(animEditorObject));
     
    Jake_sunshine likes this.
  3. Jake_sunshine

    Jake_sunshine

    Joined:
    Jan 5, 2023
    Posts:
    3

    Brilliant. This works. Thank you.
     
    vladibalan likes this.