基类,用于暂停协同程序的自定义 yield 指令。
CustomYieldInstruction 可使您实现自定义 yield 指令,
以暂停执行协同程序,直至发生事件为止。在后台,自定义 yield
指令只是另一个正在运行的协同程序。要实现该指令,应继承
CustomYieldInstruction 类,然后重写 keepWaiting 属性。要使
协同程序保持暂停,则返回 /true/。要使协同程序继续执行,则返回
/false/。在 MonoBehaviour.Update
后以及 MonoBehaviour.LateUpdate 前的每一帧均查询 keepWaiting 属性。
该类要求 Unity 5.3 或更高版本。
要使协同程序保持暂停,则返回 /true/。要使协同程序继续执行,
则返回 /false/。
// Example showing how a CustomYieldInstruction script file // can be used. This waits for the left button to go up and then // waits for the right button to go down. using System.Collections; using UnityEngine;
public class ExampleScript : MonoBehaviour { void Update() { if (Input.GetMouseButtonUp(0)) { Debug.Log("Left mouse button up"); StartCoroutine(waitForMouseDown()); } }
public IEnumerator waitForMouseDown() { yield return new WaitForMouseDown(); Debug.Log("Right mouse button pressed"); } }
以下脚本实现 keepWaiting
的可重写
版本。此 c# 实现可由 JS 使用。
在这种情况下,确保此 c# 脚本位于 Plugins
之类的文件夹中,以便使其在
上述 JS 脚本示例之前加以编译。
using UnityEngine;
public class WaitForMouseDown : CustomYieldInstruction { public override bool keepWaiting { get { return !Input.GetMouseButtonDown(1); } }
public WaitForMouseDown() { Debug.Log("Waiting for Mouse right button down"); } }
using System.Collections; using UnityEngine; using System;
// Implementation of WaitWhile yield instruction. This can be later used as: // yield return new WaitWhile(() => Princess.isInCastle); class WaitWhile1 : CustomYieldInstruction { Func<bool> m_Predicate;
public override bool keepWaiting { get { return m_Predicate(); } }
public WaitWhile1(Func<bool> predicate) { m_Predicate = predicate; } }
为获得更多控制并实现更复杂的 yield 指令,您可以直接继承 System.Collections.IEnumerator
类。在这种情况下,按照您实现 keepWaiting 属性的相同方式实现 MoveNext()
方法。此外,您还可以在 Current
属性中返回一个对象,在执行了 MoveNext()
方法后,Unity 的协同程序计划程序将处理该对象。因此,例如,如果 Current
返回了继承 IEnumerator
的另一个对象,则当前枚举器将暂停,直到返回的对象已完成为止。
using System; using System.Collections;
// Same WaitWhile implemented by inheriting from IEnumerator. class WaitWhile2 : IEnumerator { Func<bool> m_Predicate;
public object Current { get { return null; } }
public bool MoveNext() { return m_Predicate(); }
public void Reset() {}
public WaitWhile2(Func<bool> predicate) { m_Predicate = predicate; } }
keepWaiting | 指示协同程序是否应保持暂停。 |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.