版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
在 Unity 动画系统中,动画播放完毕默认是停留在最后一帧的,当我们重置场景的时候,需要将动画重置到第一帧。而 Animation 和 Animator 组件中并没有 Reset 方法。本文主要提供了关于 Animation 和 Animator 动画重置的解决方法。
Animator 重置到起始帧
通过 Animator.Play
和 Animator.Update
播放来进行控制。
Animator.Play
- 函数原型
void Play(string stateName, int layer = -1, float normalizedTime = float.NegativeInfinity);
- 参数列表
Parameters 参数 | Description 描述 |
---|---|
stateName | The name of the state the will be played. 将要播放的动画状态名字。 |
layer | The layer where the state is. 动画状态所在的层。 |
normalizedTime | The normalized time at which the state will play. 将要播放动画状态的归一化时间。 |
Animator.Update
- 函数原型
void Update(float deltaTime);
- 参数列表
Parameters 参数 | Description 描述 |
---|---|
deltaTime | The time delta. 增量时间。 |
示例代码
public void animToStart()
{
//参数:动画名,层,时间
animator.Play("Take 001", -1 ,0f);
animator.Update(0f);
}
Animaton 重置到起始帧
解决思路
通过设置动画的 AnimationState ,将其时间调整到0,从而达到重置动画的效果。
示例代码
public void ResetAni(Animation ani, string name)
{
AnimationState state = ani[name];
ani.Play(name);
state.time = 0;
ani.Sample();
state.enabled = false;
}
参考资料
【1】http://wiki.ceeger.com/script:unityengine:classes:animator:animator.play
【2】http://wiki.ceeger.com/script/unityengine/classes/animator/animator.update