相关链接:
http://www.unitymanual.com/thread-18449-1-1.html
http://docs.unity3d.com/ScriptReference/StateMachineBehaviour.html
常用API(需要继承StateMachineBehaviour):
下面以一个攻击连招的例子讲解吧!在新的动画系统中,连线变得简单明了,每增加一个动画,就像下面一样连起来就是了。至于脚本的编写,只需点击图中的任意一个动画状态,然后在Inspector面板中点击Add Behaviour,即可为该动画状态编写脚本。这里我建了一个int变量控制整个状态机的状态变换,在右边的5个退出条件中,全部取消掉Has Exit Time(退出时间改为用脚本控制)。
代码如下:
using UnityEngine; using System.Collections; public class Idle : StateMachineBehaviour { override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { if (Input.GetKeyDown(KeyCode.H)) animator.SetInteger("state", (int)CharacterState.Attack1); if ((Input.GetAxisRaw("Horizontal") != 0) || (Input.GetAxisRaw("Vertical") != 0)) animator.SetInteger("state", (int)CharacterState.Run); } }
using UnityEngine; using System.Collections; public class Attack1 : StateMachineBehaviour { override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { if (Input.GetKeyDown(KeyCode.H) && (stateInfo.normalizedTime > 0.5f)) animator.SetInteger("state", (int)CharacterState.Attack2); if(stateInfo.normalizedTime > 0.9f) animator.SetInteger("state", (int)CharacterState.Idle); } }
using UnityEngine; using System.Collections; public class Attack2 : StateMachineBehaviour { override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { if (Input.GetKeyDown(KeyCode.H) && (stateInfo.normalizedTime > 0.5f)) animator.SetInteger("state", (int)CharacterState.Attack3); if (stateInfo.normalizedTime > 0.9f) animator.SetInteger("state", (int)CharacterState.Idle); } }
using UnityEngine; using System.Collections; public class Attack3 : StateMachineBehaviour { override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { if (stateInfo.normalizedTime > 0.7f) animator.SetInteger("state", (int)CharacterState.Idle); } }
using UnityEngine; using System.Collections; public class Run : StateMachineBehaviour { override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex) { if ((Input.GetAxisRaw("Horizontal") == 0) && (Input.GetAxisRaw("Vertical") == 0)) animator.SetInteger("state", (int)CharacterState.Idle); } }
另外,可以用代码来创建AnimatorController,不过这种方法创建的AC,里面的动画状态排列得不是很美观,后期维护就蛋疼了,慎用啊。。
using UnityEngine; using UnityEditor; using UnityEditor.Animations; public class CreateController : EditorWindow { [MenuItem("Window/CreateController")] static void Create() { // Creates the controller var controller = AnimatorController.CreateAnimatorControllerAtPath ("Assets/StateMachineTransitions.controller"); // Add parameters controller.AddParameter("TransitionNow", AnimatorControllerParameterType.Trigger); controller.AddParameter("Reset", AnimatorControllerParameterType.Trigger); controller.AddParameter("GotoB1", AnimatorControllerParameterType.Trigger); controller.AddParameter("GotoC", AnimatorControllerParameterType.Trigger); // Add StateMachines var rootStateMachine = controller.layers[0].stateMachine; var stateMachineA = rootStateMachine.AddStateMachine("smA"); var stateMachineB = rootStateMachine.AddStateMachine("smB"); var stateMachineC = stateMachineB.AddStateMachine("smC"); // Add States var stateA1 = stateMachineA.AddState("stateA1"); var stateB1 = stateMachineB.AddState("stateB1"); var stateB2 = stateMachineB.AddState("stateB2"); stateMachineC.AddState("stateC1"); var stateC2 = stateMachineC.AddState("stateC2"); // don’t add an entry transition, should entry to state by default // Add Transitions var exitTransition = stateA1.AddExitTransition(); exitTransition.AddCondition(AnimatorConditionMode.If, 0, "TransitionNow"); exitTransition.duration = 0; var resetTransition = stateMachineA.AddAnyStateTransition(stateA1); resetTransition.AddCondition(AnimatorConditionMode.If, 0, "Reset"); resetTransition.duration = 0; var transitionB1 = stateMachineB.AddEntryTransition(stateB1); transitionB1.AddCondition(AnimatorConditionMode.If, 0, "GotoB1"); stateMachineB.AddEntryTransition(stateB2); stateMachineC.defaultState = stateC2; var exitTransitionC2 = stateC2.AddExitTransition(); exitTransitionC2.AddCondition(AnimatorConditionMode.If, 0, "TransitionNow"); exitTransitionC2.duration = 0; var stateMachineTransition = rootStateMachine.AddStateMachineTransition(stateMachineA, stateMachineC); stateMachineTransition.AddCondition(AnimatorConditionMode.If, 0, "GotoC"); rootStateMachine.AddStateMachineTransition(stateMachineA, stateMachineB); } }