Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    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:
    524
  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.