检查动画剪辑是否已完成 [英] Check if animation clip has finished

查看:38
本文介绍了检查动画剪辑是否已完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是update里面的简单代码.遗憾的是它对我不起作用.甚至我已经将环绕模式设置为永久夹紧.

This is the simple code inside of update. sadly it is not working for me. Even I have set Wrap Mode to clamp Forever.

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

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

How can I check that animation clip have finished so that I can do some work? I don't want to use WaitForSeconds method, because I am working on Time and each time a new animation will be played,

推荐答案

基本是这样...

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

You can trivially google thousands of QA about this example, http://answers.unity3d.com/answers/37440/view.html

如果您不完全了解 Unity 中的协程,请退后一步并查看网络上有关Unity 中的协程"的大约 8,000 个完整的初学者教程和 QA.

If you do not fully understand coroutines in Unity, step back and review any of the some 8,000 full beginner tutorials and QA regarding "coroutines in Unity" on the web.

这确实是在 Unity 中检查动画是否完成的方法 - 这是一种标准技术,确实别无选择.

This is really the way to check an animation is finished in Unity - it's a standard technique and there's really no alternative.

你提到你想在 Update() 中做一些事情......你可以做这样的事情:

You mention you want to do something in Update() ... you could possibly do something like this:

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

我强烈建议您使用协程来实现;如果您尝试始终使用更新",您最终会陷入困境".希望有帮助.

I strongly encourage you to just do it with a coroutine; you'll end up "in knots" if you try to "always use Update". Hope it helps.

仅供参考,您还提到实际上场景是:在指定时间(时间/时钟分别运行).我必须播放动画,这就是为什么我使用更新来检查时间并相应地运行动画"

这很容易做到,假设您想从现在开始运行动画 3.721 秒...

This is very easy to do, say you want to run the animation 3.721 seconds from now...

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

这篇关于检查动画剪辑是否已完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆