Version: 2020.3

WaitWhile

class in UnityEngine

/

继承自:CustomYieldInstruction

切换到手册

描述

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

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

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

using UnityEngine;
using System.Collections;

public class WaitWhileExample : MonoBehaviour { public int frame;

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

IEnumerator Example() { Debug.Log("Waiting for prince/princess to rescue me..."); yield return new WaitWhile(() => frame < 10); Debug.Log("Finally I have been rescued!"); }

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

构造函数

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

继承的成员

变量

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