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

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

问题描述

这是更新中的简单代码。不幸的是,它对我不起作用。

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

您可以对此Google进行数千次质量检查例如 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中的协程,请退后一步,回顾网上有关8,000个完整的初学者教程和质量检查中的任何内容。

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.

仅供参考,您还提到了实际上,情况是:在指定时间(时间/时钟)我必须播放一个动画,这就是为什么我使用更新检查时间并相应地运行动画的原因。

FYI you also mention "actually the scenario is: at specified time (Time/clock separately running). I have to play an animation thats why i am using update to check the time and run animation accordingly"

这非常容易做到,假设您要从现在开始运行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天全站免登陆