Unity教程之-Unity5中的动画系统(一)

 

本篇文章我们来看下Unity5中的动画系统,常用API(需要继承StateMachineBehaviour):

Unity5中的动画系统

下面以一个攻击连招的例子讲解吧!在新的动画系统中,连线变得简单明了,每增加一个动画,就像下面一样连起来就是了。至于脚本的编写,只需点击图中的任意一个动画状态,然后在Inspector面板中点击Add Behaviour,即可为该动画状态编写脚本。这里我建了一个int变量控制整个状态机的状态变换,在右边的5个退出条件中,全部取消掉Has Exit Time(退出时间改为用脚本控制)。

Unity5中的动画系统

代码如下:

Idle .cs


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);
}

}

Attack1 .cs


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);
}

}

Attack2 .cs


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);
}

}

Run .cs


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);
}

}

效果图:

Unity5中的动画系统

另外,可以用代码来创建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);
}
}

好了,本篇unity3d教程到此结束,下篇我们再会!