Unity3D 旧版Animation. Time.timeScale = 0暂停,播放动画.

需要用到Time.timeScale = 0暂停,但个别地方需要播放动画,在Time.timeScale = 0前,开启下面的"播放动画"协程即可.

IEnumerator PlayAnimation(Animation animation, string clipName, bool useTimeScale, System.Action onComplete)
{
    if (!useTimeScale)
    {
        AnimationState _currState = animation[clipName];
        bool isPlaying = true;
        float _progressTime = 0F;
        float _timeAtLastFrame = 0F;
        float _timeAtCurrentFrame = 0F;
        float deltaTime = 0F;
        animation.Play(clipName);
        _timeAtLastFrame = Time.realtimeSinceStartup;
        while (isPlaying)
        {
            _timeAtCurrentFrame = Time.realtimeSinceStartup;
            deltaTime = _timeAtCurrentFrame - _timeAtLastFrame;
            _timeAtLastFrame = _timeAtCurrentFrame;

            _progressTime += deltaTime;
            _currState.normalizedTime = _progressTime / _currState.length;
            if (_progressTime >= _currState.length)
            {
                if (_currState.wrapMode != WrapMode.Loop)
                {
                    isPlaying = false;
                }
                else
                {
                    _progressTime = 0.0f;
                }
                Debug.Log("gui 0");
                _progressTime = 0.0f;
            }
            yield return new WaitForEndOfFrame();
        }
        yield return null;
        if (onComplete != null)
        {
            onComplete();
        }
    }
    else
    {
        animation.Play(clipName);
    }
}

动画片段设置为Loop,停止"播放协程"貌似不能暂停动画播放.
要再开启一个"关闭协程".

  IEnumerator StopAnimation(Animation animation, string clipName)
  {
      yield return null;
      animation.Stop(clipName);
  }

注意:Animation Clip 要在Debug模式下,设置为Legacy.
或者用Notepad++打开Animation Clip文件,把 m_Legacy: 0 设置为 m_Legacy: 1

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值