基本上是这样的……
PlayAnimation(); // Whatever you are calling or using for animation
StartCoroutine(WaitForAnimation(animation));
private IEnumerator WaitForAnimation ( Animation animation )
{
while(animation.isPlaying)
yield return null;
// at this point, the animation has completed
// so at this point, do whatever you wish...
Debug.Log("Animation completed");
}
您可以轻松地在谷歌上搜索 数千个关于此的 QA 示例,http://answers.unity3d.com/answers/37440/view.html
如果您不完全了解 Unity 中的协程,请退后一步,回顾一下网络上有关“Unity 中的协程”的大约 8,000 个完整的初学者教程和 QA。
这确实是在 Unity 中检查动画是否完成的方法 - 这是一种标准技术,确实没有其他选择。
您提到您想在Update() 中做某事……您可以这样做:
private Animation trainGoAnime=ation;
public void Update()
{
if ( trainGoAnimation.isPlaying() )
{
Debug.Log("~ the animation is still playing");
return;
}
else
{
Debug.Log("~ not playing, proceed normally...");
}
}
我强烈建议您只使用协程;如果您尝试“始终使用更新”,您最终会“陷入困境”。希望对您有所帮助。
仅供参考,您还提到“实际上场景是:在指定时间(时间/时钟分别运行)。我必须播放动画,这就是为什么我使用更新来检查时间并相应地运行动画” em>
这很容易做到,假设你想从现在开始运行动画 3.721 秒...
private float whenToRunAnimation = 3.721;
就这么简单!
// run horse anime, with pre- and post- code
Invoke( "StartHorseAnimation", whenToRunAnimation )
private void StartHorseAnimation()
{
Debug.Log("starting horse animation coroutine...");
StartCoroutine(_horseAnimationStuff());
}
private IENumerator _horseAnimationStuff()
{
horseA.Play();
Debug.Log("HORSE ANIME BEGINS");
while(horseA.isPlaying)yield return null;
Debug.Log("HORSE ANIME ENDS");
... do other code here when horse anime ends ...
... do other code here when horse anime ends ...
... do other code here when horse anime ends ...
}