【Unity3D】Animation 和 Animator 动画重置到起始帧的方法

在 Unity 动画系统中,动画播放完毕默认是停留在最后一帧的,当我们重置场景的时候,需要将动画重置到第一帧。而 Animation 和 Animator 组件中并没有 Reset 方法。本文主要提供了关于 Animation 和 Animator 动画重置的解决方法。

Animator 重置到起始帧

通过 Animator.PlayAnimator.Update 播放来进行控制。

Animator.Play

  • 函数原型
void Play(string stateName, int layer = -1, float normalizedTime = float.NegativeInfinity);
  • 参数列表
Parameters 参数Description 描述
stateNameThe name of the state the will be played.
将要播放的动画状态名字。
layerThe layer where the state is.
动画状态所在的层。
normalizedTimeThe normalized time at which the state will play.
将要播放动画状态的归一化时间。

Animator.Update

  • 函数原型
void Update(float deltaTime);
  • 参数列表
Parameters 参数Description 描述
deltaTimeThe 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

  • 13
    点赞
  • 48
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机灵鹤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值