Search Unity

  1. Are you interested in providing feedback directly to Unity teams? Sign up to become a member of Unity Pulse, our new product feedback and research community.
    Dismiss Notice

Can you use coroutines like this?

Discussion in 'Scripting' started by TOES, Oct 5, 2020.

  1. TOES

    TOES

    Joined:
    Jun 23, 2017
    Posts:
    94
    My coroutine looks like this:

     public IEnumerator heavyTask()
    {
    Texture2D tex1=generateTexture(parameters...);
    yield return null;
    Texture2D tex2=generateTexture(parameters...);
    yield return null;
    ...
    }


    The problem is, generateTexure is a very heavy task that can take minutes, and I need it to yield during the steps it takes.

    How do I add yields inside that generateTexture method, as if I unrolled the method into the calling coroutine and used yield there?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    6,141
    You'd have to do exactly that:
    add yields inside that generateTexture method
    , in other words, turn that method itself into a coroutine, then you could yield for it in this coroutine like this:
    Code (CSharp):
    1. yield return generateTexture(parameters...);
    Of course you'd have to pass in a ref variable or something to get results out of it.
     
    TOES likes this.
unityunity