[英]Check if animation clip has finished
这是update里面的简单代码。 遗憾的是它对我不起作用。 甚至我已经将环绕模式设置为永远夹紧。
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");
}
您可以轻松地搜索有关此示例的数千个 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 ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.