LoginSignup
0
1

More than 5 years have passed since last update.

[Unity] WaitUntilが常にtrueを返すときに1フレーム待機するか?

Last updated at Posted at 2017-04-08

概要

コルーチンの中でWaitUntilを使用するとtrueになるまで待機する。

常にtrueを返す場合、yield return nullのように1フレーム待機するのか?

環境

Unity5.5.2p4

結果

待機する。

実験内容

Unityのスクリプトリファレンスには以下のように書かれている。

Supplied delegate will be executed each frame after script MonoBehaviour.Update and before MonoBehaviour.LateUpdate. When the delegate finally evaluates to true, the coroutine will proceed with its execution.

UpdateとLateUpdateの間で実行される。
この間にログを出力して確認する。

using UnityEngine;
using System.Collections;

public class TestWaitUntil : MonoBehaviour
{
    private void Start()
    {
        StartCoroutine(Loop());
        StartCoroutine(Loop2());
    }

    private void Update()
    {
        Debug.Log("Update()");
    }

    private void LateUpdate()
    {
        Debug.Log("LateUpdate()");
    }

    IEnumerator Loop()
    {
        WaitUntil wait = new WaitUntil(() => { return true; });

        while(true)
        {
            Debug.Log("Loop() Start WaitUntil.");
            yield return wait;
            Debug.Log("Loop() End WaitUntil.");
        }
    }

    IEnumerator Loop2()
    {
        while(true)
        {
            Debug.Log("Loop2() Start.");
            yield return null;
            Debug.Log("Loop2() End.");
        }
    }
}

ログ出力

Loop() Start WaitUntil.
Loop2() Start.
Update()
LateUpdate()
Update()
Loop() End WaitUntil.
Loop() Start WaitUntil.
Loop2() End.
Loop2() Start.
LateUpdate()
Update()
Loop() End WaitUntil.
Loop() Start WaitUntil.
Loop2() End.
Loop2() Start.

あとがき

当たり前シリーズです:wink:
yield文があるから待機しますよね。
じゃないと無限ループになっちゃうし。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1