private void Start() { int layerCount = m_animator.layerCount; // Initialize animator layer names. m_animatorLayerNames = new string[layerCount]; for (int i = 0; i < layerCount; i++) { m_animatorLayerNames[i] = m_animator.GetLayerName(i); } m_previousTransitions = new AnimatorTransitionInfo[layerCount]; m_previousStates = new AnimatorStateInfo[layerCount]; m_animatorStateData = new AnimatorStateData[layerCount]; m_animatorDefaultStates = new AnimatorStateData[layerCount]; // Set up actions list. int count = m_controller.CharActions.Length; m_actions = new CharacterAction[count]; for (int i = 0; i < count; i++) { m_actions[i] = m_controller.CharActions[i]; } // // Setup all the animatior state data. // AnimatorController animatorController = m_animator.runtimeAnimatorController as AnimatorController; for (int i = 0; i < animatorController.layers.Length; i++) { AnimatorStateMachine stateMachine = animatorController.layers[i].stateMachine; AnimatorState defaultState = stateMachine.defaultState; if (defaultState == null) { defaultState = stateMachine.AddState("Empty", Vector3.zero); } m_animatorStateData[i] = new AnimatorStateData(defaultState.nameHash, defaultState.name, 0.2f); m_animatorDefaultStates[i] = new AnimatorStateData(defaultState.nameHash, defaultState.name, 0.2f); } // // Register all m_animator states. // foreach (AnimatorControllerLayer layer in animatorController.layers) { RegisterAnimatorStates(layer.stateMachine, layer.name); } SetHeight(1); }
/// <summary> /// Determine the state that the specified layer should be in. /// </summary> /// <param name="layer">The layer to determine the state of.</param> /// <param name="defaultState">The default state to be in if no other states should run.</param> /// <param name="checkAction">Should the action be checked to determine if they have control?</param> /// <param name="baseStart">Is the base layer being set at the same time?</param> /// <returns>True if the state was changed.</returns> public virtual bool DetermineState(int layer, AnimatorStateData defaultState, bool checkAction, bool baseStart = false) { bool stateChanged = false; // pull our current transition and state into temporary variables, since we may not need to do anything with them AnimatorTransitionInfo currentTransition = m_animator.GetAnimatorTransitionInfo(layer); AnimatorStateInfo currentState = m_animator.GetCurrentAnimatorStateInfo(layer); // if we have a new transition... int previousTransition = m_previousTransitions[layer].fullPathHash; if (currentTransition.fullPathHash != previousTransition) { //Debug.LogFormat("Transitioning. normalized time: {0}", currentTransition.normalizedTime); //if (m_activeStateAction) //{ // // Determine if the character action has control of the animator. // if (checkAction) { // if (m_activeStateAction.HasAnimatorControl(layer)) { // // State action has control, so animator is not transitioning to new action state. // return false; // } // } //} //// -- Determine next animator state. // Check if character has an item equipped. string nextState = FormatStateName(layer, m_controller.isMoving ? m_controller.MoveStateName : defaultState.StateName); int stateHash = GetStateHash(nextState); if (layer == 0) { Debug.LogFormat("<b>[Transitioning]</b> Next state is <color=blue>{0}</color> ({1}", nextState, stateHash); } //if (stateHash != -1) { // m_animator.CrossFade(stateHash, 0.2f, layer, 0); //} else { // m_animator.CrossFade(Animator.StringToHash(defaultState.StateName), 0.2f, layer, 0); //} // fire off our end callback, if any, for our previous transition... // Next state is starting. // fire off our begin call back for our new transition... // and remember that we are now in this transition. m_previousTransitions[layer] = currentTransition; } // if we have a new state, things go similarly. int previousState = m_previousStates[layer].fullPathHash; if (currentState.fullPathHash != previousState) { if (layer == 0) { if (debugStateChanges) { Debug.LogFormat("Transitioning from <color=blue>{0}</color> to <color=blue>{1}</color>.", GetStateName(previousState), GetStateName(currentState.fullPathHash)); } } // Current state is ending. // Next state is starting. //if(m_activeStateAction != null) //{ // //Debug.Break(); // m_controller.TryStopAction(m_activeStateAction, true); // m_activeStateAction = null; //} // recall what state we were in m_previousStates[layer] = currentState; // State has changed. stateChanged = true; } Delegate endCallback; if (m_stateEndCallbackMap.TryGetValue(previousState, out endCallback)) { ((Callback)endCallback)(); } Delegate beginCallback; if (m_stateBeginCallbackMap.TryGetValue(currentState.fullPathHash, out beginCallback)) { ((Callback)beginCallback)(); } return(stateChanged); }