1

Update場合によっては、 -functionで再生されるアニメーションがありますSwitch

アニメーションが終了すると、ブール値がtrueに設定されます。

私のコード:

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

問題は私のものgoboolstartbool、アニメーションを終了せずにすぐに設定されます。アニメーションが終了するまでプログラムを待機させるにはどうすればよいですか?

4

1 に答える 1

7

基本的に、このソリューションを機能させるには、次の2つのことを行う必要があります。

  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ループは、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();
    }
}

最後に、これをコードで簡単に使用できます。

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

ソース、代替案および議論。

于 2013-02-27T11:12:11.753 に答える