需要用到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