c# - 检查动画剪辑是否已完成

标签 c# animation unity3d

这是更新中的简单代码。可悲的是它不适合我。甚至我已经将 Wrap Mode 设置为永远固定。

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

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

最佳答案

基本上是这样的...

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");
    }

你可以简单地用谷歌搜索关于这个的数以千计的质量检查,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 ...
  }

关于c# - 检查动画剪辑是否已完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35030418/

相关文章:

c# - 在 Unity 中双向旋转门

android - 如何淡出 TextView 中的单个字符

java - 如何让碰撞和动画在 2d 平台游戏中发挥作用?

c# - XML 序列化类结果为空 XML

c# - 如何在谷歌云平台控制台中启用 'Vector Tile API'?

reactjs - 如何使用react-bootstrap制作加载动画

c# - 如何在外部点击时关闭弹出布局?

c# - 使用 C# 显示相关内容的最佳方式

c# - 是否存在永远不会匹配任何字符串的正则表达式?

c# - 在白色中按类查找桌面窗口