Version: 2020.2

WaitUntil

class in UnityEngine

/

继承自:CustomYieldInstruction

切换到手册

描述

暂停协程执行,直到提供的委托评估为 /true/。

在协程中,WaitUntil 只能与 yield 语句结合使用。

每帧都会执行提供的委托,在脚本 MonoBehaviour.Update 之后 MonoBehaviour.LateUpdate 之前执行。当委托最终评估为 true 时,协程将继续其执行。

另请参阅:AsyncOperationWaitForEndOfFrameWaitForFixedUpdateWaitForSecondsWaitForSecondsRealtimeWaitWhile

using UnityEngine;
using System.Collections;

public class WaitUntilExample : MonoBehaviour { public int frame;

void Start() { StartCoroutine(Example()); }

IEnumerator Example() { Debug.Log("Waiting for princess to be rescued..."); yield return new WaitUntil(() => frame >= 10); Debug.Log("Princess was rescued!"); }

void Update() { if (frame <= 10) { Debug.Log("Frame: " + frame); frame++; } } }

构造函数

WaitUntil使用要评估的给定委托实例化一个 yield 指令。

继承的成员

变量

keepWaiting指示协同程序是否应保持暂停。