Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

168
Vistas
Universal type for any yield type

Trying to have a system able to store any kind of yield instructions

YieldInstruction m_yield;

void SetInstruction()
{
    switch(condition)
    {
        case UseWait:
            m_yield = new WaitForSeconds(1f);
            break;
        case UseWaitUntil:
            m_yield = new WaitUntil(() => flag == true); // Cannot cast here
    }
}

Changing the type to IEnumerator puts the problem on the first one. CustomeYieldInstruction is not doing either.

I can't put my finger on what is the relation between YieldInstruction and CustomYieldInstruction. Despite the names, one is its own base type and the latter is IEnumerator.

I'm also confused since the two methods can yield in a IEnumerator method but won't cast into it if done as I am trying.

public sealed class WaitForSeconds : YieldInstruction{ /* ** */ }
public class YieldInstruction { }

public sealed class WaitUntil : CustomYieldInstruction { /* ** */}
public abstract class CustomYieldInstruction : IEnumerator { /* ** */ }

and I can do :

public IEnumerator Sequence()
{
     yield return new WaitForSeconds(1f),
     yield return new WaitUntil(()=> condition == true);
}
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

In addition to Menyus' answer

I'd say using object is a bit too dirty for my taste.

But you could simply build a wrapper IEnumerator for those types that do not implement it themselves (like anything based on the internal times):

private IEnumerator WaitForYieldInstruction(YieldInstruction instruction)
{
    yield return instruction;
}

and then use

IEnumerator m_yield;

void SetInstruction()
{
    switch(condition)
    {
        case UseWait:
            m_yield = WaitForYieldInstruction(new WaitForSeconds(1f));
            break;
        case UseWaitUntil:
            m_yield = new WaitUntil(() => flag); 
            break;
    }
}
over 4 years ago · Santiago Trujillo Denunciar

0

YieldInstruction gets special treatments from Unity's internal system. If you just look at WaitUntil it needs no special treatments from the internal Engine(C/C++) since it just expects a C# predicate. On the other hand WaitForSeconds, WaitForEndOfFrame ofc needs some special treatments. In the former one it needs the internal timer, in the latter one the coroutine scheduler have to make a special interrupt just at the end of frame which is kinda impossible with just the managed scripting side.

Also do not forget you can yield anything! null also does not implement IEnumerator nor string etc..

When you call StartCoroutine(MyCoroutine); you are basically registering the method into Unity's coroutine scheduler and it is implemented in C/C++.

Back to your original question. Luckily in C# every class inherits from System.Object so they can have a base class.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda