Unity脚本设置Animator单个状态的speed
直接上代码:
private Animator anim;
private AnimatorController animController;
private void Awake()
{
anim = GetComponent<Animator>();
animController = anim.runtimeAnimatorController as AnimatorController;
if (animController)
{
//传入layerIndex 要更改速度的状态名称和speed
SetAnimatorSpeed(0, "TestState", 2f);
}
}
/// <summary>
/// 在运行时更改Speed 运行结束时不会恢复到之前的Speed 对文件进行了更改
/// 如果要恢复 在需要的时机再调用一次方法
/// </summary>
private void SetAnimatorSpeed(int _layer, string _stateName, float _speed)
{
for (int i = 0; i < animController.layers[_layer].stateMachine.states.Length; i++)
{
if (animController.layers[_layer].stateMachine.states[i].state.name == _stateName)
{
animController.layers[_layer].stateMachine.states[i].state.speed = _speed;
}
}
}
private void OnDestroy()
{
SetAnimatorSpeed(0, "TestState", 1f);
}