检查动画剪辑是否已完成



这是更新中的简单代码。 可悲的是它对我不起作用。即使我已经将包装模式设置为永远夹住。

 if (trainGO.GetComponent<Animation>()["Start"].time >= trainGO.GetComponent<Animation>()["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 中的协程,

请后退一步,查看网络上有关"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 ...
  }

最新更新