FAnimationTrackInspector.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditor.Animations;
  4. //using UnityEditorInternal;
  5. using System.Collections.Generic;
  6. using Flux;
  7. using FluxEditor;
  8. namespace FluxEditor
  9. {
  10. [CustomEditor(typeof(FAnimationTrack))]
  11. public class FAnimationTrackInspector : FTrackInspector {
  12. // private const string ADVANCE_TRIGGER = "FAdvanceTrigger";
  13. private const string FLUX_STATE_MACHINE_NAME = "FluxStateMachine";
  14. private FAnimationTrack _animTrack = null;
  15. private SerializedProperty _animatorController = null;
  16. private SerializedProperty _layerName = null;
  17. private SerializedProperty _layerId = null;
  18. public override void OnEnable()
  19. {
  20. base.OnEnable();
  21. if( target == null )
  22. {
  23. DestroyImmediate( this );
  24. return;
  25. }
  26. _animTrack = (FAnimationTrack)target;
  27. _animatorController = serializedObject.FindProperty("_animatorController");
  28. _layerName = serializedObject.FindProperty("_layerName");
  29. _layerId = serializedObject.FindProperty( "_layerId" );
  30. }
  31. public override void OnInspectorGUI()
  32. {
  33. base.OnInspectorGUI();
  34. serializedObject.Update();
  35. if( _animatorController.objectReferenceValue == null )
  36. {
  37. EditorGUILayout.HelpBox("There's no Animator Controller", MessageType.Warning);
  38. Rect helpBoxRect = GUILayoutUtility.GetLastRect();
  39. float yCenter = helpBoxRect.center.y;
  40. helpBoxRect.xMax -= 3;
  41. helpBoxRect.xMin = helpBoxRect.xMax - 50;
  42. helpBoxRect.yMin = yCenter - 12.5f;
  43. helpBoxRect.height = 25f;
  44. if( GUI.Button( helpBoxRect, "Create" ) )
  45. {
  46. AnimatorController newController = CreateAnimatorController( _animTrack );
  47. if( newController )
  48. _animatorController.objectReferenceValue = newController;
  49. UpdateLayer( newController == null ? null : newController.layers[0] );
  50. }
  51. }
  52. AnimatorController prevAnimatorController = (AnimatorController)_animatorController.objectReferenceValue;
  53. EditorGUILayout.PropertyField( _animatorController );
  54. AnimatorController controller = (AnimatorController)_animatorController.objectReferenceValue;
  55. if( controller != prevAnimatorController )
  56. {
  57. AnimatorControllerLayer layer = controller == null ? null : controller.layers[0];
  58. if( layer != null && (layer.stateMachine.states.Length > 0 || layer.stateMachine.stateMachines.Length > 0) )
  59. layer = null;
  60. UpdateLayer( layer );
  61. }
  62. if( controller != null )
  63. {
  64. string[] layers = new string[controller.layers.Length];
  65. int layerIndex = -1;
  66. for( int i = 0; i != controller.layers.Length; ++i )
  67. {
  68. layers[i] = controller.layers[i].name;
  69. if( layers[i] == _layerName.stringValue )
  70. layerIndex = i;
  71. }
  72. // doesn't have layer
  73. if( layerIndex == -1 )
  74. {
  75. EditorGUILayout.HelpBox("No Layer Selected!", MessageType.Error);
  76. }
  77. else if( layerIndex != _layerId.intValue ) // has it, but it got moved
  78. {
  79. UpdateLayer( controller.layers[layerIndex] );
  80. }
  81. EditorGUI.BeginChangeCheck();
  82. layerIndex = EditorGUILayout.Popup( "State Machine Layer", layerIndex, layers );
  83. if( EditorGUI.EndChangeCheck() )
  84. {
  85. // choosing existing one
  86. if( layerIndex > -1 )
  87. {
  88. if( VerifyUseAnimatorControllerLayer( controller.layers[layerIndex] ) )
  89. {
  90. UpdateLayer( controller.layers[layerIndex] );
  91. }
  92. }
  93. }
  94. }
  95. serializedObject.ApplyModifiedProperties();
  96. }
  97. public void UpdateLayer( AnimatorControllerLayer layer )
  98. {
  99. if( layer == null )
  100. {
  101. _layerName.stringValue = null;
  102. _layerId.intValue = -1;
  103. }
  104. else
  105. {
  106. AnimatorController controller = (AnimatorController)_animatorController.objectReferenceValue;
  107. for( int i = 0; i != controller.layers.Length; ++i )
  108. {
  109. if( controller.layers[i].name == layer.name )
  110. {
  111. _layerName.stringValue = layer.name;
  112. _layerId.intValue = i;
  113. return;
  114. }
  115. }
  116. // shouldn't get here..
  117. Debug.LogError("Trying to set a layer that doesn't belong to the controller");
  118. UpdateLayer( null );
  119. }
  120. }
  121. public static void RebuildStateMachine( FAnimationTrack track )
  122. {
  123. if( track.AnimatorController == null || track.LayerId == -1 )
  124. return;
  125. bool isPreviewing = track.HasCache;
  126. if( isPreviewing )
  127. track.ClearCache();
  128. Animator animator = track.Owner.GetComponent<Animator>();
  129. animator.runtimeAnimatorController = null;
  130. AnimatorController controller = (AnimatorController)track.AnimatorController;
  131. track.UpdateEventIds();
  132. AnimatorControllerLayer layer = FindLayer( controller, track.LayerName );
  133. if( layer == null )
  134. {
  135. controller.AddLayer( track.LayerName );
  136. layer = FindLayer( controller, track.LayerName );
  137. }
  138. AnimatorStateMachine fluxSM = FindStateMachine( layer.stateMachine, FLUX_STATE_MACHINE_NAME );
  139. if( fluxSM == null )
  140. {
  141. fluxSM = layer.stateMachine.AddStateMachine( FLUX_STATE_MACHINE_NAME );
  142. ChildAnimatorStateMachine[] stateMachines = layer.stateMachine.stateMachines;
  143. for( int i = 0; i != stateMachines.Length; ++i )
  144. {
  145. if( stateMachines[i].stateMachine == fluxSM )
  146. {
  147. stateMachines[i].position = layer.stateMachine.entryPosition + new Vector3( 300, 0, 0 );
  148. break;
  149. }
  150. }
  151. layer.stateMachine.stateMachines = stateMachines;
  152. }
  153. List<FEvent> events = track.Events;
  154. if( fluxSM.states.Length > events.Count )
  155. {
  156. for( int i = events.Count; i < fluxSM.states.Length; ++i )
  157. fluxSM.RemoveState( fluxSM.states[i].state );
  158. }
  159. else if( fluxSM.states.Length < events.Count )
  160. {
  161. for( int i = fluxSM.states.Length; i < events.Count; ++i )
  162. fluxSM.AddState( i.ToString() );
  163. }
  164. AnimatorState lastState = null;
  165. Vector3 pos = fluxSM.entryPosition + new Vector3( 300, 0, 0 );
  166. Vector3 statePosDelta = new Vector3(0, 80, 0 );
  167. ChildAnimatorState[] states = fluxSM.states;
  168. for( int i = 0; i != events.Count; ++i )
  169. {
  170. FPlayAnimationEvent animEvent = (FPlayAnimationEvent)events[i];
  171. if( animEvent._animationClip == null ) // dump events without animations
  172. continue;
  173. states[i].position = pos;
  174. AnimatorState state = states[i].state;
  175. state.name = i.ToString();
  176. pos += statePosDelta;
  177. state.motion = animEvent._animationClip;
  178. animEvent._stateHash = Animator.StringToHash( state.name );
  179. if( lastState )
  180. {
  181. AnimatorStateTransition[] lastStateTransitions = lastState.transitions;
  182. if( animEvent.IsBlending() )
  183. {
  184. AnimatorStateTransition transition = null;
  185. for( int j = lastStateTransitions.Length-1; j > 0; --j )
  186. {
  187. lastState.RemoveTransition( lastStateTransitions[j] );
  188. }
  189. transition = lastStateTransitions.Length == 1 ? lastStateTransitions[0] : lastState.AddTransition( state );
  190. transition.offset = (animEvent._startOffset / animEvent._animationClip.frameRate) / animEvent._animationClip.length;
  191. FPlayAnimationEvent prevAnimEvent = (FPlayAnimationEvent)events[i-1];
  192. animEvent._startOffset = Mathf.Clamp( Mathf.RoundToInt(transition.offset * animEvent._animationClip.length * animEvent._animationClip.frameRate), 0, animEvent._animationClip.isLooping ? animEvent.Length : Mathf.RoundToInt( animEvent._animationClip.length * animEvent._animationClip.frameRate ) - animEvent.Length );
  193. transition.offset = (animEvent._startOffset / animEvent._animationClip.frameRate) / animEvent._animationClip.length;
  194. EditorUtility.SetDirty( animEvent );
  195. transition.duration = (animEvent._blendLength / prevAnimEvent._animationClip.frameRate) / prevAnimEvent._animationClip.length;
  196. transition.hasExitTime = true;
  197. transition.exitTime = (prevAnimEvent.Length + prevAnimEvent._startOffset) / (prevAnimEvent._animationClip.length*prevAnimEvent._animationClip.frameRate);
  198. for( int j = transition.conditions.Length-1; j >= 0; --j )
  199. {
  200. transition.RemoveCondition( transition.conditions[j] );
  201. }
  202. // AnimatorCondition condition = transition.conditions[0];
  203. // condition.threshold = 0;
  204. }
  205. else // animations not blending, needs hack for animation previewing
  206. {
  207. for( int j = lastStateTransitions.Length-1; j >= 0; --j )
  208. {
  209. lastState.RemoveTransition( lastStateTransitions[j] );
  210. }
  211. }
  212. }
  213. lastState = state;
  214. }
  215. fluxSM.states = states;
  216. if( fluxSM.states.Length > 0 )
  217. layer.stateMachine.defaultState = fluxSM.states[0].state;
  218. animator.runtimeAnimatorController = controller;
  219. if( isPreviewing )
  220. track.CreateCache();
  221. }
  222. public static AnimatorStateTransition GetTransitionTo( FPlayAnimationEvent animEvt )
  223. {
  224. FAnimationTrack animTrack = (FAnimationTrack)animEvt.Track;
  225. if( animTrack.AnimatorController == null )
  226. return null;
  227. AnimatorController controller = (AnimatorController)animTrack.AnimatorController;
  228. AnimatorState animState = null;
  229. AnimatorControllerLayer layer = FindLayer( controller, ((FAnimationTrack)animEvt.Track).LayerName );
  230. if( layer == null )
  231. return null;
  232. if( layer.stateMachine.stateMachines.Length == 0 )
  233. return null;
  234. ChildAnimatorStateMachine fluxSM = layer.stateMachine.stateMachines[0];
  235. AnimatorStateMachine stateMachine = fluxSM.stateMachine;
  236. for( int i = 0; i != stateMachine.states.Length; ++i )
  237. {
  238. if( stateMachine.states[i].state.nameHash == animEvt._stateHash )
  239. {
  240. animState = stateMachine.states[i].state;
  241. break;
  242. }
  243. }
  244. if( animState == null )
  245. {
  246. // Debug.LogError("Couldn't find state " + animEvt._animationClip );
  247. return null;
  248. }
  249. for( int i = 0; i != stateMachine.states.Length; ++i )
  250. {
  251. AnimatorState state = stateMachine.states[i].state;
  252. AnimatorStateTransition[] transitions = state.transitions;
  253. for( int j = 0; j != transitions.Length; ++j )
  254. {
  255. if( transitions[j].destinationState == animState )
  256. {
  257. return transitions[j];
  258. }
  259. }
  260. }
  261. return null;
  262. }
  263. public static AnimatorController CreateAnimatorController( FAnimationTrack animTrack )
  264. {
  265. string defaultFolder = "Assets/";
  266. string defaultFileName = string.Format( "{0}_{1}.controller", animTrack.Timeline.Sequence.name, animTrack.Owner.name );
  267. string defaultFilePath = AssetDatabase.GenerateUniqueAssetPath( defaultFolder+defaultFileName );
  268. defaultFileName = System.IO.Path.GetFileNameWithoutExtension( defaultFilePath );
  269. string filePath = EditorUtility.SaveFilePanel( "Create new AnimatorController...", defaultFolder, defaultFileName, "controller" );
  270. if( string.IsNullOrEmpty( filePath ) )
  271. return null;
  272. // transform the path into a local path
  273. if( !filePath.StartsWith( Application.dataPath ) )
  274. {
  275. Debug.LogError("Cannot save controller outside of the project");
  276. return null;
  277. }
  278. string fileLocalPath = "Assets"+filePath.Substring(Application.dataPath.Length, filePath.Length-Application.dataPath.Length);
  279. // AnimatorController animatorAtPath = (AnimatorController)AssetDatabase.LoadAssetAtPath( fileLocalPath, typeof(AnimatorController) );
  280. return AnimatorController.CreateAnimatorControllerAtPath( fileLocalPath );
  281. }
  282. private static bool VerifyUseAnimatorControllerLayer( AnimatorControllerLayer layer )
  283. {
  284. if( layer == null || (layer.stateMachine.states.Length == 0 && layer.stateMachine.stateMachines.Length == 0) )
  285. return true;
  286. return EditorUtility.DisplayDialog( "Animator Controller's Layer isn't empty!",
  287. "Layer '" + layer.name + "' is not empty, and Flux will change it.\n"
  288. + "Are you sure you want to use this layer?", "Use", "Cancel" );
  289. }
  290. private static AnimatorControllerParameter FindParameter( AnimatorController controller, string paramName )
  291. {
  292. foreach( AnimatorControllerParameter p in controller.parameters )
  293. {
  294. if( p.name == paramName )
  295. return p;
  296. }
  297. return null;
  298. }
  299. private static AnimatorControllerLayer FindLayer( AnimatorController controller, string layerName )
  300. {
  301. foreach( AnimatorControllerLayer layer in controller.layers )
  302. {
  303. if( layer.name == layerName )
  304. return layer;
  305. }
  306. return null;
  307. }
  308. private static AnimatorStateMachine FindStateMachine( AnimatorStateMachine stateMachine, string stateMachineName )
  309. {
  310. foreach( ChildAnimatorStateMachine sm in stateMachine.stateMachines )
  311. {
  312. if( sm.stateMachine.name == stateMachineName )
  313. return sm.stateMachine;
  314. }
  315. return null;
  316. }
  317. }
  318. }