AnimationWindowProxy.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System;
  4. using System.Reflection;
  5. namespace FluxEditor
  6. {
  7. public class AnimationWindowProxy {
  8. private static Type ANIMATION_WINDOW_TYPE = typeof(EditorWindow).Assembly.GetType("UnityEditor.AnimationWindow");
  9. private static Type ANIMATION_WINDOW_STATE_TYPE = typeof(EditorWindow).Assembly.GetType("UnityEditorInternal.AnimationWindowState");
  10. private static Type ANIMATION_SELECTION_TYPE = typeof(EditorWindow).Assembly.GetType("UnityEditor.AnimationSelection");
  11. private static Type ANIMATION_EDITOR_TYPE = typeof(EditorWindow).Assembly.GetType("UnityEditor.AnimEditor");
  12. private static Type ANIMATION_WINDOW_SELECTED_ITEM_TYPE = typeof(EditorWindow).Assembly.GetType("UnityEditorInternal.AnimationWindowSelectionItem");
  13. private static EditorWindow _animationWindow = null;
  14. public static EditorWindow AnimationWindow {
  15. get {
  16. if( _animationWindow == null )
  17. _animationWindow = FUtility.GetWindowIfExists( ANIMATION_WINDOW_TYPE );
  18. return _animationWindow;
  19. }
  20. }
  21. public static EditorWindow OpenAnimationWindow()
  22. {
  23. if( _animationWindow == null )
  24. _animationWindow = EditorWindow.GetWindow( ANIMATION_WINDOW_TYPE );
  25. return _animationWindow;
  26. }
  27. #region AnimationWindow variables
  28. private static FieldInfo _animEditorField = null;
  29. private static FieldInfo AnimEditorField {
  30. get {
  31. if( _animEditorField == null )
  32. _animEditorField = ANIMATION_WINDOW_TYPE.GetField( "m_AnimEditor", BindingFlags.Instance | BindingFlags.NonPublic );
  33. return _animEditorField;
  34. }
  35. }
  36. private static ScriptableObject _animEditor = null;
  37. private static ScriptableObject AnimEditor {
  38. get {
  39. if( _animEditor == null )
  40. _animEditor = (ScriptableObject)AnimEditorField.GetValue( AnimationWindow );
  41. return _animEditor;
  42. }
  43. }
  44. private static FieldInfo _stateField = null;
  45. private static FieldInfo StateField {
  46. get {
  47. if( _stateField == null )
  48. _stateField = ANIMATION_EDITOR_TYPE.GetField("m_State", BindingFlags.Instance | BindingFlags.NonPublic);
  49. return _stateField;
  50. }
  51. }
  52. private static PropertyInfo _selectedItemProperty = null;
  53. private static PropertyInfo SelectedItemProperty {
  54. get{
  55. if( _selectedItemProperty == null )
  56. _selectedItemProperty = ANIMATION_EDITOR_TYPE.GetProperty("selectedItem", BindingFlags.Instance | BindingFlags.Public);
  57. return _selectedItemProperty;
  58. }
  59. }
  60. private static PropertyInfo _animationClipProperty = null;
  61. private static PropertyInfo AnimationClipProperty {
  62. get {
  63. if( _animationClipProperty == null )
  64. _animationClipProperty = ANIMATION_WINDOW_SELECTED_ITEM_TYPE.GetProperty("animationClip", BindingFlags.Instance | BindingFlags.Public);
  65. return _animationClipProperty;
  66. }
  67. }
  68. #endregion
  69. #region AnimationWindowState variables
  70. private static PropertyInfo _currentTimeProperty = null;
  71. private static PropertyInfo CurrentTimeProperty {
  72. get {
  73. if( _currentTimeProperty == null )
  74. _currentTimeProperty = ANIMATION_WINDOW_STATE_TYPE.GetProperty("currentTime", BindingFlags.Instance | BindingFlags.Public);
  75. return _currentTimeProperty;
  76. }
  77. }
  78. private static PropertyInfo _frameProperty = null;
  79. private static PropertyInfo FrameProperty {
  80. get {
  81. if( _frameProperty == null )
  82. _frameProperty = ANIMATION_WINDOW_STATE_TYPE.GetProperty("currentFrame", BindingFlags.Instance | BindingFlags.Public);
  83. return _frameProperty;
  84. }
  85. }
  86. private static PropertyInfo _recordingProperty = null;
  87. private static PropertyInfo RecordingProperty {
  88. get {
  89. if( _recordingProperty == null )
  90. _recordingProperty = ANIMATION_WINDOW_STATE_TYPE.GetProperty("recording", BindingFlags.Instance | BindingFlags.Public);
  91. return _recordingProperty;
  92. }
  93. }
  94. #endregion
  95. #region AnimationSelection variables
  96. private static MethodInfo _chooseClipMethod = null;
  97. private static MethodInfo ChooseClipMethod {
  98. get {
  99. if( _chooseClipMethod == null )
  100. _chooseClipMethod = ANIMATION_SELECTION_TYPE.GetMethod( "ChooseClip", BindingFlags.Instance | BindingFlags.NonPublic, null, new Type[]{typeof(AnimationClip)}, null );
  101. return _chooseClipMethod;
  102. }
  103. }
  104. #endregion
  105. public static void StartAnimationMode()
  106. {
  107. //RecordingProperty.SetValue( GetState(), true, null );
  108. AnimationMode.StartAnimationMode();
  109. }
  110. private static object GetState()
  111. {
  112. return StateField.GetValue( AnimEditor );
  113. }
  114. private static object GetSelectedItem()
  115. {
  116. return SelectedItemProperty.GetValue(AnimEditor, null);
  117. }
  118. public static void SetCurrentFrame( int frame, float time )
  119. {
  120. if( AnimationWindow == null )
  121. return;
  122. object state = GetState();
  123. // pass a little nudge because of floating point errors which will make it go
  124. // to a lower frame instead of the frame we want
  125. CurrentTimeProperty.SetValue( state, time+0.0001f, null );
  126. _animationWindow.Repaint();
  127. }
  128. public static int GetCurrentFrame()
  129. {
  130. if( AnimationWindow == null )
  131. return -1;
  132. return (int)FrameProperty.GetValue( GetState(), null );
  133. }
  134. public static void SelectAnimationClip( AnimationClip clip )
  135. {
  136. if( AnimationWindow == null || clip == null )
  137. return;
  138. AnimationClip[] clips = AnimationUtility.GetAnimationClips(Selection.activeGameObject);
  139. int index = 0;
  140. for( ; index != clips.Length; ++index )
  141. {
  142. if( clips[index] == clip )
  143. break;
  144. }
  145. if( index == clips.Length )
  146. {
  147. // didn't find
  148. Debug.LogError("Couldn't find clip " + clip.name);
  149. }
  150. else
  151. {
  152. // found
  153. AnimationClipProperty.SetValue( GetSelectedItem(), clip, null );
  154. }
  155. }
  156. public static AnimationClip GetSelectedAnimationClip()
  157. {
  158. return AnimationClipProperty.GetValue(GetSelectedItem(), null) as AnimationClip;
  159. }
  160. }
  161. }