动画参数 (Animation Parameters) 是在动画系统中定义的变量,但也可以是脚本的访问值和分配值。例如,参数值可由动画曲线更新,然后从脚本访问,因此,比如说,音效的音高就像动画一样会改变。同样,脚本可以设置 Mecanim 选择的参数值。例如,脚本可以设置一个参数来控制混合树。
默认参数值可以使用“动画器 (Animator)” 窗口左下角的参数 (Parameters) 小部件来设置。它们有四种基本类型:
参数可以是使用动画器 (Animator) 类中函数的脚本的分配值:SetVector、SetFloat、SetInt 和 SetBool。
以下是基于用户输入修改参数的脚本示例
using UnityEngine; using System.Collections; public class AvatarCtrl :MonoBehaviour { protected Animator animator; public float DirectionDampTime = .25f; void Start () { animator = GetComponent<Animator>(); } void Update () { if(animator) { //get the current state AnimatorStateInfo stateInfo = animator.GetCurrentAnimatorStateInfo(0); //if we're in "Run" mode, respond to input for jump, and set the Jump parameter accordingly. if(stateInfo.nameHash == Animator.StringToHash("Base Layer.RunBT")) { if(Input.GetButton("Fire1")) animator.SetBool("Jump", true ); } else { animator.SetBool("Jump", false); } float h = Input.GetAxis("Horizontal"); float v = Input.GetAxis("Vertical"); //set event parameters based on user input animator.SetFloat("Speed", h*h+v*v); animator.SetFloat("Direction", h, DirectionDampTime, Time.deltaTime); } } }
(返回动画状态机)
Page last updated: 2013-06-24