c# - 等待动画在 unity3d 中完成

标签 c# animation camera boolean unity3d

Switch 情况下,我有一个在 Update 函数中播放的动画。

动画结束后, boolean 值被设置为 true。

我的代码:

case "play":    
    animation.Play("play");     
    gobool = true;
    startbool = false;
    break;

问题是我的 goboolstartbool 在没有完成动画的情况下立即设置。如何让我的程序等到动画完成?

最佳答案

基本上您需要做两件事才能使该解决方案起作用:

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

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

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 的同一帧中返回 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();
    }
}

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

IEnumerator Start()
{
    yield return animation.WhilePlaying("Something");
}

Source, alternatives and discussion.

关于c# - 等待动画在 unity3d 中完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15110201/

相关文章:

c# - 将 JSON 字符串添加到 C# 中的现有字符串

c# - DataGridView——突出显示具有迟到项目的行

c# - 如何从遗留代码中删除共享变量

apache-flex - Flex 与 DHTML 有哪些区别?

javascript - 如何覆盖 requestAnimationFrame 循环?

c++ - (光线追踪)转换为屏幕坐标时出现问题,对象被拉伸(stretch)

iphone - 为什么 AVCaptureVideoDataOutput 回调依赖于 OpenGL 绘制帧速率?

c# - 如何使这个异步? (异步、等待 - C#、MVC)

ios - 滑动窗口打开动画无法正常工作 [Titanium]

ios - 在 IOS 应用程序中构建相机时出错