- Home /
Can you turn off auto keyframe in the animation window?
I have a component on an object that only runs in edit mode. This script helps me align my objects by making them look at a point and does this each frame they are selected so I can move them around easily. I also have added an animation to my object. The problem is that when I scrub my animation the script updates the objects rotation and that automatically gets key framed in the animation.
Is there anyway to turn off auto key framing in the animation window? Or is there a way to check if the editor is in animation record mode so i can disable my component from code?
Answer by Bunny83 · Jun 14, 2011 at 03:48 AM
Well, that's a very special case. Unfortunately AnimationWindow is an internal class, so you can't access it directly. The second problem is that you can't search for the instance of the opened AnimationWindow. The only way i've found is that you check EditorWindow.focusedWindow and when you focus the animation window you can "grab" the reference. With reflection you can call the function GetAutoRecordMode of the animation window to get the record state.
This little helper function will only work in Editor scripts and only when the AnimationWindow is focused.
Note: This is a pure hack :D you should avoid such things.
// C#
bool GetAutoRecordMode()
{
UnityEditor.EditorWindow W = UnityEditor.EditorWindow.focusedWindow;
System.Type T = W.GetType();
if (T.Name == "AnimationWindow")
{
System.Object Res = T.InvokeMember("GetAutoRecordMode",System.Reflection.BindingFlags.InvokeMethod,null,W,null);
return ((bool)Res);
}
return false;
}
I have it finally perfect :D The function works always. It search for the animation window and if it's there it get the record state, otherwise it will return false as well.
bool GetAutoRecordMode()
{
System.Type T = System.Type.GetType("UnityEditor.AnimationWindow,UnityEditor");
Object[] allAniWindows = Resources.FindObjectsOfTypeAll(T);
if (allAniWindows.Length > 0)
{
UnityEditor.EditorWindow W = (UnityEditor.EditorWindow)allAniWindows[0];
System.Object Res = T.InvokeMember("GetAutoRecordMode", System.Reflection.BindingFlags.InvokeMethod, null, W, null);
return ((bool)Res);
}
return false;
}
Thanks to ILSpy i've figured out how to "find" an EditorWindow ;)
Btw: there's also a "SetAutoRecord$$anonymous$$ode" function. If you want to enable/disable the record mode.
Thanks a bunch. Yeah I was going to go about it this way myself. But your code will be put to good use. Thanks again.
Hi, Bunny83! Can You explain where i must use this code, that he began to work? Thanks!
@dragonikus: $$anonymous$$y answer actually answers his alternative question:
Or is there a way to check if the editor is in animation record mode so i can disable my component from code?
You don't have to use this anywhere ;) It just tells you if the Animation window of the editor is in autorecord mode or not. It can of course only be used in an editor script. It seems he implemented some additional edit-features for his own stuff and needed to know when the auto-record mode is active to disable his editor script so it doesn't get messed up with the animation recording.
I just found some new functions in the AnimationUtility class. It provides a function to get the animationmode and a function to start or stop the animation mode.
So my "hack" is no longer necessary ;)
Your answer
![](https://koobas.hobune.stream/wayback/20220612104518im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mecanim recording 1 Answer
How do you add an animation to an imported 3D model? 1 Answer
Can the animation editor create local rotational data? 3 Answers
How to Record or Play an animation backwards 1 Answer
Adding animation clips via script 2 Answers