class in UnityEngine
/
Implemented in:UnityEngine.CoreModule
Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.
CloseFor some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.
CloseBase class for custom yield instructions to suspend coroutines.
CustomYieldInstruction lets you implement custom yield instructions
to suspend coroutine execution until an event happens. Under the hood, custom yield
instruction is just another running coroutine. To implement it, inherit from
CustomYieldInstruction class and override keepWaiting property. To keep
coroutine suspended return true
. To let coroutine proceed with execution return
false
. keepWaiting property is queried each frame after MonoBehaviour.Update
and before MonoBehaviour.LateUpdate.
This class requires Unity 5.3 or later.
To keep coroutine suspended, return true
. To let coroutine proceed
with execution, return 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"); } }
The following script implements the overridable version of
keepWaiting
. This c# implementation can be used by JS.
In this case make sure this c# script is in a folder such as Plugins
so it is
compiled before the JS script example above.
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; } }
To have more control and implement more complex yield instructions you can inherit directly from System.Collections.IEnumerator
class. In this case, implement MoveNext()
method the same way you would implement keepWaiting property. Additionally to that, you can also return an object in Current
property, that will be processed by Unity's coroutine scheduler after executing MoveNext()
method. So for example if Current
returned another object inheriting from IEnumerator
, then current enumerator would be suspended until the returned one has completed.
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 | Indicates if coroutine should be kept suspended. |
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.