等待动画在unity3d中完成

Son*_*ona 1 c# animation camera boolean unity-game-engine

Update在一个Switch案例中,我有一个动画在-function 中播放.

动画结束后,将布尔值设置为true.

我的代码:

case "play":    
    animation.Play("play");     
    gobool = true;
    startbool = false;
    break;
Run Code Online (Sandbox Code Playgroud)

问题是我的,goboolstartbool立即设置而不完成动画.我怎样才能让我的程序等到动画结束?

Joe*_*jah 7

基本上,您需要为此解决方案做两件事:

  1. 开始动画.
  2. 在播放下一个动画之前等待动画完成.

如何做到这一点的一个例子是:

animation.PlayQueued("Something");
yield WaitForAnimation(animation);
Run Code Online (Sandbox Code Playgroud)

而定义WaitForAnimation将是:

C#:

private IEnumerator WaitForAnimation (Animation animation)
{
    do
    {
        yield return null;
    } while (animation.isPlaying);
}
Run Code Online (Sandbox Code Playgroud)

JS:

function WaitForAnimation (Animation animation)
{
    yield; while ( animation.isPlaying ) yield;
}
Run Code Online (Sandbox Code Playgroud)

do-while循环来自实验,表明调用同一帧PlayQueued 中的animation.isPlaying返回false值.

通过一些修补,您可以为动画创建一个扩展方法,简化了这种方法,例如:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

最后,您可以在代码中轻松使用它:

IEnumerator Start()
{
    yield return animation.WhilePlaying("Something");
}
Run Code Online (Sandbox Code Playgroud)

来源,替代方案和讨论.