等待动画在unity3d完成 [英] Wait for an animation to finish in unity3d
问题描述
我有在更新
-function扮演,在切换动画
情况。
I have an animation which plays in the Update
-function, in a Switch
case.
在动画完成后,一个布尔值被设置为true。
After the animation finishes, a Boolean is being set to true.
我的code:
case "play":
animation.Play("play");
gobool = true;
startbool = false;
break;
问题是我的 gobool
和 startbool
立即得到设定没有完成的动画。我怎样才能让我的程序等待,直到动画完成?
The problem is my gobool
and startbool
get set immediately without finishing the animation. How can I let my program wait untill the animation is finished?
推荐答案
基本上你需要做两件事情对这个解决方案的工作:
Basically you need to do two things for this solution to work:
- 开始动画。
- 等待动画播放下一个动画之前完成。
怎么可以这样做的一个例子是:
An example of how this could be done is:
animation.PlayQueued("Something");
yield WaitForAnimation(animation);
和定义为 WaitForAnimation
是:
C#:
private IEnumerator WaitForAnimation (Animation animation)
{
do
{
yield return null;
} while (animation.isPlaying);
}
JS:
function WaitForAnimation (Animation animation)
{
yield; while ( animation.isPlaying ) yield;
}
在do-while循环来自该实验的结果表明, animation.isPlaying
收益假
在同一帧PlayQueued被调用。
The do-while loop came from experiments that showed that the animation.isPlaying
returns false
in the same frame PlayQueued is called.
通过稍微修改一下就可以创建一个动画扩展方法,简化了这个功能,比如:
With a little tinkering you can create a extension method for animation that simplifies this such as:
public static class AnimationExtensions
{
public static IEnumerator WhilePlaying( this Animation animation )
{
do
{
yield return null;
} while ( animation.isPlaying );
}
public static IEnumerator WhilePlaying( this Animation animation,
string animationName )
{
animation.PlayQueued(animationName);
yield return animation.WhilePlaying();
}
}
最后,你可以很容易地在code使用:
Finally you can easily use this in code:
IEnumerator Start()
{
yield return animation.WhilePlaying("Something");
}
<一个href=\"http://answers.unity3d.com/questions/37411/how-can-i-wait-for-an-animation-to-complete.html\">Source,选择和讨论。
这篇关于等待动画在unity3d完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!