Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

knowing when Animation Window is in record mode in script?

Discussion in 'Animation' started by FeastSC2, Aug 29, 2018.

  1. FeastSC2

    FeastSC2

    Joined:
    Sep 30, 2016
    Posts:
    540
  2. bagle45

    bagle45

    Joined:
    Jul 25, 2016
    Posts:
    11
    There doesn't appear to be any canonical way to do this, but it can be done with reflection if you don't mind the performance hit:
    Code (CSharp):
    1.  
    2. bool isAnimWindowRecording ()
    3. {
    4.    System.Reflection.Assembly editorAssembly = typeof (Editor).Assembly;
    5.  
    6.    System.Type windowType = editorAssembly.GetType
    7.        ("UnityEditorInternal.AnimationWindowState");
    8.  
    9.    // Get a reference to the unbound property we want to access.
    10.    System.Reflection.PropertyInfo isRecordingProp = windowType.GetProperty
    11.        ("recording", System.Reflection.BindingFlags.Instance |
    12.        System.Reflection.BindingFlags.Public);
    13.  
    14.    // Get all instances of objects of type "AnimationWindowState"
    15.    // (There should be exactly one of these, unless the window is closed)
    16.    Object [] windowInstances = Resources.FindObjectsOfTypeAll (windowType);
    17.  
    18.    for (int i = 0; i < windowInstances.Length; i++)
    19.    {
    20.        bool isRecording = (bool) isRecordingProp.GetValue
    21.            (windowInstances [i], null);
    22.  
    23.        //Debug.Log ("isRec: " + isRecording);
    24.  
    25.        return isRecording;
    26.    }
    27.  
    28.    // No instances found, so we'll presume the window is closed.
    29.    return false;
    30. }
    31.  
    Disclamer: this code leverages unity's internal api which is not intended to be accessed this way, and could change without warning at any point.
     
    FeastSC2 likes this.