场景:
Animator是Unity中的一个动画制作工具,可用于创建和控制游戏中的动画效果。以下是Animator中常用的几个函数:
方法
Play(string stateName):播放指定名称的动画状态。
SetTrigger(string triggerName):设置指定名称的触发器。
ResetTrigger(string triggerName):重置指定名称的触发器。
GetBool(string propertyName):获取指定名称的布尔型参数的值。
SetBool(string propertyName, bool value):设置指定名称的布尔型参数的值。
GetFloat(string propertyName):获取指定名称的浮点型参数的值。
SetFloat(string propertyName, float value):设置指定名称的浮点型参数的值。
GetInteger(string propertyName):获取指定名称的整型参数的值。
SetInteger(string propertyName, int value):设置指定名称的整型参数的值。
例子
using UnityEngine;
public class PlayerController : MonoBehaviour
{
// 获取Animator组件
private Animator animator;
// 初始化
void Start()
{
animator = GetComponent<Animator>();
}
// 更新
void Update()
{
// 播放名为“Walk”的动画状态
animator.Play("Walk");
// 设置名为“JumpTrigger”的触发器
if (Input.GetKeyDown(KeyCode.Space))
{
animator.SetTrigger("JumpTrigger");
}
// 获取名为“IsRunning”的布尔型参数的值
bool isRunning = animator.GetBool("IsRunning");
// 设置名为“IsRunning”的布尔型参数的值为true
animator.SetBool("IsRunning", true);
// 获取名为“Speed”的浮点型参数的值
float speed = animator.GetFloat("Speed");
// 设置名为“Speed”的浮点型参数的值为5.0f
animator.SetFloat("Speed", 5.0f);
// 获取名为“JumpCount”的整型参数的值
int jumpCount = animator.GetInteger("JumpCount");
// 设置名为“JumpCount”的整型参数的值为3
animator.SetInteger("JumpCount", 3);
}
}