概要
コルーチンの中で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.
あとがき
当たり前シリーズです
yield文があるから待機しますよね。
じゃないと無限ループになっちゃうし。