【问题标题】:Check if animation clip has finished检查动画剪辑是否完成
【发布时间】:2016-01-27 06:41:27
【问题描述】:

这是更新内部的简单代码。可悲的是它不适合我。甚至我都将 Wrap Mode 设置为永远钳制。

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

如何检查动画剪辑是否已完成以便我可以做一些工作?我不想使用 WaitForSeconds 方法,因为我正在处理 Time 并且每次都会播放一个新动画,

【问题讨论】:

标签: c# animation unity3d


【解决方案1】:

基本上是这样的……

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 ...
  }

【讨论】:

  • 我知道协程,我不想用它
  • 我正在更新中播放动画。检查时间然后相应地播放动画
  • 嗨 Mohammad - 你能解释一下为什么你不喜欢使用协程吗?我总是告诉人们“如果不需要,不要使用协程”,但是在这种情况下,这是唯一的解决方案。请解释您要避免的情况。干杯!
  • 首先有两个原因,我正在检查动画是否完成的更新,并且协同程序在更新中无法正常工作
  • 有更简单的方法。当您发布新问题时,请在此处记下,以便这里的人可以看到新问题,祝您好运!
猜你喜欢
  • 2014-10-25
  • 2012-02-26
  • 2019-08-26
  • 1970-01-01
  • 2015-04-05
  • 2020-05-02
  • 1970-01-01
  • 2021-05-25
  • 1970-01-01
相关资源
最近更新 更多