Version: 5.3 (switch to 5.4b)
IdiomaEnglish
  • C#
  • JS

Idioma de script

Selecciona tu lenguaje de programación favorito. Todos los fragmentos de código serán mostrados en este lenguaje.

CustomYieldInstruction

class in UnityEngine

Sugiere un cambio

¡Éxito!

Gracias por ayudarnos a mejorar la calidad de la documentación de Unity. A pesar de que no podemos aceptar todas las sugerencias, leemos cada cambio propuesto por nuestros usuarios y actualizaremos los que sean aplicables.

Cerrar

No se puedo enviar

Por alguna razón su cambio sugerido no pudo ser enviado. Por favor <a>intente nuevamente</a> en unos minutos. Gracias por tomarse un tiempo para ayudarnos a mejorar la calidad de la documentación de Unity.

Cerrar

Cancelar

Cambiar al Manual

Descripción

Base 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.

no example available in JavaScript
using UnityEngine;

// Implementation of WaitWhile yield instruction. This can be later used as: // yield return new WaitWhile(() => Princess.isInCastle); class WaitWhile: CustomYieldInstruction { Func<bool> m_Predicate;

public override bool keepWaiting { get { return m_Predicate(); } }

public WaitWhile(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. Additionaly 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.

no example available in JavaScript
// Same WaitWhile implemented by inheriting from IEnumerator.
class WaitWhile: IEnumerator {
	Func<bool> m_Predicate;

public object Current { get { return null; } }

public bool MoveNext() { return m_Predicate(); }

public void Reset() {}

public WaitWhile(Func<bool> predicate) { m_Predicate = predicate; } }

Variables

keepWaitingIndicates if coroutine should be kept suspended.