检查动画片段是否已完成

这是更新内部的简单代码。 可悲的是,它不适合我。 事件我已设置环绕模式永远夹紧!

if (trainGO.GetComponent()["Start"].time >= trainGO.GetComponent()["Start"].length) { blackTrain.SetActive(true); redTrain.SetActive(false); } 

如何检查动画片段是否已完成,以便我可以完成一些工作。 我不想使用WaitForSeconds方法。 因为我在时间上徘徊,每次都会播放新的动画

基本上这样……

 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中的协同程序,请退一步查看大约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..."); } } 

我强烈建议你用协程来做; 如果您尝试“始终使用更新”,您将以“结”结束。 希望能帮助到你。


仅供参考,你还提到“实际情况是:在指定的时间(时间/时间单独运行)。我必须播放动画,这就是为什么我使用更新来检查时间并相应地运行动画”

这很容易做到,比如你想从现在开始运行动画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 ... }