Last active
March 2, 2017 11:50
-
-
Save codewings/08ca386fe90d6faace1b7548fbc541d4 to your computer and use it in GitHub Desktop.
Adds some useful hotkeys to the Animation window in Unity.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Tested on Unity 5.3.5 & Unity 5.5.1 | |
using UnityEngine; | |
using UnityEditor; | |
using UnityEditorInternal; | |
using System.Collections; | |
using System.Reflection; | |
[ExecuteInEditMode] | |
public class AnimationWindowHotkeys : Editor | |
{ | |
static System.Type animWindowType = null; | |
static System.Type animEditorType = null; | |
static System.Type GetAnimationWindowType() | |
{ | |
if( animWindowType == null ) | |
{ | |
animWindowType = System.Type.GetType( "UnityEditor.AnimationWindow,UnityEditor" ); | |
} | |
return animWindowType; | |
} | |
static System.Type GetAnimEditorType() | |
{ | |
if( animEditorType == null ) | |
{ | |
animEditorType = System.Type.GetType( "UnityEditor.AnimEditor,UnityEditor" ); | |
} | |
return animEditorType; | |
} | |
static Object GetAnimationWindow() | |
{ | |
Object[] openAnimationWindows = Resources.FindObjectsOfTypeAll(GetAnimationWindowType ()); | |
if ( openAnimationWindows.Length > 0 ) | |
{ | |
return openAnimationWindows[0]; | |
} | |
return null; | |
} | |
static object GetAnimEditor() | |
{ | |
var wndType = GetAnimationWindowType (); | |
Object[] openAnimationWindows = Resources.FindObjectsOfTypeAll(wndType); | |
if ( openAnimationWindows.Length > 0 ) | |
{ | |
return wndType.GetField("m_AnimEditor", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(openAnimationWindows[0]); | |
} | |
return null; | |
} | |
static void RepaintAnimationWindow() | |
{ | |
Object w = GetAnimationWindow(); | |
if ( w != null ) | |
{ | |
( w as EditorWindow ).Repaint(); | |
} | |
} | |
[MenuItem( "GlobalHotKeys/Animation/Prev Keyframe #," )] | |
static void PrevFrame() | |
{ | |
var w = GetAnimEditorType().GetField("m_State", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(GetAnimEditor()); | |
if (w != null) | |
{ | |
var t = w.GetType(); | |
var p = t.GetProperty("frame", BindingFlags.Instance | BindingFlags.Public); | |
var v = (int)p.GetValue(w, new object[]{ }); | |
if (v <= 0) | |
return; | |
p.SetValue (w, v - 1, new object[] { }); | |
RepaintAnimationWindow(); | |
} | |
} | |
[MenuItem( "GlobalHotKeys/Animation/Next Keyframe #." )] | |
static void NextFrame() | |
{ | |
var w = GetAnimEditorType().GetField("m_State", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(GetAnimEditor()); | |
if (w != null) | |
{ | |
var t = w.GetType(); | |
var p = t.GetProperty("frame", BindingFlags.Instance | BindingFlags.Public); | |
var v = (int)p.GetValue(w, new object[]{ }); | |
p.SetValue (w, v + 1, new object[] { }); | |
RepaintAnimationWindow(); | |
} | |
} | |
[MenuItem( "GlobalHotKeys/Animation/Prev Keyframe &," )] | |
static void PrevKeyframe() | |
{ | |
var w = GetAnimEditor(); | |
if ( w != null ) | |
{ | |
GetAnimEditorType().InvokeMember( "MoveToPreviousKeyframe", BindingFlags.InvokeMethod | BindingFlags.NonPublic, null, w, null ); | |
RepaintAnimationWindow(); | |
} | |
} | |
[MenuItem( "GlobalHotKeys/Animation/Next Keyframe &." )] | |
static void NextKeyframe() | |
{ | |
var w = GetAnimEditor(); | |
if ( w != null ) | |
{ | |
GetAnimEditorType().InvokeMember( "MoveToNextKeyframe", BindingFlags.InvokeMethod | BindingFlags.NonPublic, null, w, null ); | |
RepaintAnimationWindow(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment