重点 ——
(0)状态机与子状态机的切换,状态之间的切换
(1)状态调用的切换 AnimatorStateTransition、状态机调用的切换 AnimatorTransition
(2)添加状态的先后顺序,决定了该状态机或子状态机的默认状态,但也可以用 stateMachineA.defaultState来直接指定
(3)从父状态机切换到子状态机,entry —-> stateX,会选择一个满足条件的状态
(4)从子状态机 exit,会切换到父状态机,entry —-> 默认状态,不需要满足条件(为啥)
(5)有时候会看到三个箭头的情况,有些是因为有多个切换(子状态机中包含子状态机),有些是因为 AnimatorStateMachine.AddEntryTransition ,连到一个子状态机中的默认状态(三箭头黄线)
这样看,大致上可以这样理解 ——
1、AnimatorController (动画控制器)负责管理
层 AnimatorControllerLayer、
参数 AnimatorControllerParameter、
为动画状态 AnimatorState 指定 动画剪辑 AnimationClip、
为动画状态 AnimatorState 指定 StateMachineBehaviour(AnimatorState的组件)
(还有一个 AddClip 也可以直接添加动画状态)
2、AnimatorStateMachine (动画状态机)负责管理
动画状态之间的切换 AnimatorTransition
添加动画状态 AnimatorState
添加动画状态机 AnimatorStateMachine
添加StateMachineBehaviour ———— AddStateMachineBehaviour( Type xx )
//注意添加动画状态机的写法,一般是用 根状态机来加其他的,每个状态机就是一个“页面”
var rootStateMachine = controller.layers[0].stateMachine;
var stateMachineA = rootStateMachine.AddStateMachine("smA");
var stateMachineB = rootStateMachine.AddStateMachine("smB");
var stateMachineC = stateMachineB.AddStateMachine("smC");
3、AnimatorTransition (动画状态切换)
继承 AnimatorTransitionBase,主要是
AnimatorStateMachine.AddStateMachineTransition
和 AnimatorStateMachine.AddEntryTransition的时候 ,返回类型为 AnimatorTransition,这个主要负责状态机之间的切换
例如
AnimatorStateMachine rootStateMachine = controller.layers[0].stateMachine;
//root添加 smA 和 smB , smB 添加 smC
rootStateMachine.AddStateMachineTransition(smA,smB);
rootStateMachine.AddStateMachineTransition(smA,smC);
4、AnimatorCondition (动画状态切换条件)
public AnimatorConditionMode mode; //If IfNot Greater Less Equal NotEqual
public string parameter;
public float threshold; //条件为真的阈值
通常是,添加一个Transition,返回一个 AnimatorTransition 或 AnimatorStateTransition 实例,再用这个实例来 AddCondition,例如
var rootStateMachine = controller.layers[0].stateMachine;
var resetTransition = rootStateMachine.AddAnyStateTransition(stateA1);
resetTransition.AddCondition(AnimatorConditionMode.If, 0, "Reset");
resetTransition.duration = 0;
从API看出 AnimatorStateMachine 类的大致结构如下 ——
using UnityEngine;
nameSpace UnityEdior.Animations
{
public class AnimatorStateMachine : object
{
public Vector3 anyStatePosition; //AnyState节点的坐标
public AnimatorStateTransition[] anyStateTransitions; //AnyState的切换列表
public Vector3 entryPosition; //entry 节点的坐标
public AnimatorTransition[] entryTransitions; //entry的切换列表
public Vector3 exitPosition; //exit 节点的坐标
public Vector3 parentStateMachinePosition; //父状态机节点的坐标(只在分层的状态机中有效)
public ChildAnimatorStateMachine[] stateMachines; //该状态机的子状态机的列表
public ChildAnimatorState[] states; //该状态机的子状态的列表
public StateMachineBehaviour[] behaviours; //这个状态机的状态的全部StateMachineBehaviour
public AnimatorState defaultState; //默认的动画状态(开始时就会进这个状态)
//----------------------------------------
//添加、移除动画状态的转换
AddAnyStateTransition / RemoveAnyStateTransition
AddEntryTransition / RemoveEntryTransition
AddStateMachineExitTransition / 这里就没有remove
//添加动画状态机之间、状态机与状态之间的转换
AddStateMachineTransition
//获取、设置 StateMachineTransition ----------
GetStateMachineTransitions / SetStateMachineTransitions
//添加、移除状态、状态机
AddState / RemoveState
AddStateMachine / RemoveStateMachine
//添加、移除 StateMachineBehaviour
AddStateMachineBehaviour / RemoveStateMachineBehaviour
MakeUniqueStateMachineName
MakeUniqueStateName
}
}
Animator类的文档例子
下边是对该例子的简单改写 ——
using UnityEngine;
using UnityEditor;
using UnityEditor.Animations;
//状态机调用切换 ,返回值 类型 Animator Transition
//状态 调用切换,返回值 类型 Animator State Transition
public class MyCreateStateMachine : MonoBehaviour {
static AnimatorController animController;
static AnimatorStateMachine smA;
static AnimatorStateMachine smB;
static AnimatorStateMachine smC;
[MenuItem("MyMenu/CreateMyStateMachine")]
static void Create()
{
animController = AnimatorController.CreateAnimatorControllerAtPath("Assets/Mecanim/MyController.controller");
AddParameters();
CreateRootStateMachine();
CreateStateMachineA();
CreateStateMachineB();
CreateStateMachineC();
}
static void AddParameters()
{
animController.AddParameter("TransitionNow", AnimatorControllerParameterType.Trigger);
animController.AddParameter("Reset", AnimatorControllerParameterType.Trigger);
animController.AddParameter("GotoB1", AnimatorControllerParameterType.Trigger);
animController.AddParameter("GotoC", AnimatorControllerParameterType.Trigger);
}
static void CreateRootStateMachine()
{
AnimatorStateMachine rootStateMachine = animController.layers[0].stateMachine;
smA = rootStateMachine.AddStateMachine("smA");
smB = rootStateMachine.AddStateMachine("smB");
smC = smB.AddStateMachine("smC");
AnimatorTransition stateMachineTransition = rootStateMachine.AddStateMachineTransition(smA, smC); // 状态机之间的切换
stateMachineTransition.AddCondition(AnimatorConditionMode.If, 0, "GotoC");
rootStateMachine.AddStateMachineTransition(smA, smB);
}
static void CreateStateMachineA()
{
AnimatorState stateA1 = smA.AddState("stateA1");
AnimatorStateTransition exitTransition = stateA1.AddExitTransition(); //xx state Transition
exitTransition.AddCondition(AnimatorConditionMode.If, 0, "TransitionNow");
exitTransition.duration = 0;
}
static void CreateStateMachineB()
{
AnimatorState stateB1 = smB.AddState("stateB1"); //添加的先后顺序决定了默认状态
AnimatorState stateB2 = smB.AddState("stateB2");
AnimatorTransition transitionB1 = smB.AddEntryTransition(stateB1); // Entry --- B1 xx Transition
transitionB1.AddCondition(AnimatorConditionMode.If, 0, "GotoB1");
AnimatorTransition transitionB2 = smB.AddEntryTransition(stateB2); // Entry --- B2
}
static void CreateStateMachineC()
{
AnimatorState stateC1 = smC.AddState("stateC1");
AnimatorState stateC2 = smC.AddState("stateC2");
//通过这里指定默认状态,所以上边即使先写stateC1也没用,默认状态为 stateC2
//默认状态,如果不需要指定 entry 到这个默认状态的条件,那么这句就可以达到切换的效果
smC.defaultState = stateC2;
// smC.AddEntryTransition(stateC2); //如果加了这句,就会有三个箭头的黄线
AnimatorStateTransition exitTransitionC2 = stateC2.AddExitTransition(); // xx State Transition
exitTransitionC2.AddCondition(AnimatorConditionMode.If, 0, "TransitionNow");
exitTransitionC2.duration = 0;
}
}