Unity脚本设置Animator单个状态的speed
直接上代码:
private Animator anim;
private AnimatorController animController;
private void Awake()
{
anim = GetComponent<Animator>();
animController = anim.runtimeAnimatorController as AnimatorController;
if (animController)
{
SetAnimatorSpeed(0, "TestState", 2f);
}
}
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);
}
|