Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda