More organized way to call Coroutines?

后端 未结 1 1295
遇见更好的自我
遇见更好的自我 2020-11-30 14:54

In my code, I have multiple web requests needed to be called after the previous one is finished. For example:

void Init()
{
    StartCoroutine(FirstRequest()         


        
相关标签:
1条回答
  • 2020-11-30 15:00

    This is very straightforward. Just use yield return SecondRequest(); or yield return StartCoroutine( SecondRequest());. The yield before the the coroutine name or StartCoroutine should make it wait for that coroutine to return before it continue to execute other code below it.

    For example, you have four coroutine functions that should be called sequentially:

    IEnumerator FirstRequest()
    {
        www = new WWW(my_url);
        yield return www;
    }
    
    IEnumerator SecondRequest()
    {
        www = new WWW(my_url);
        yield return www;
    }
    
    IEnumerator ThirdRequest()
    {
        www = new WWW(my_url);
        yield return www;
    }
    
    IEnumerator FourthRequest()
    {
        www = new WWW(my_url);
        yield return www;
    }
    

    You can then do this:

    void Init()
    {
        StartCoroutine(doSequentialStuff());
    }
    
    IEnumerator doSequentialStuff()
    {
        //Do first request then wait for it to return
        yield return FirstRequest();
    
        //Do second request then wait for it to return
        yield return SecondRequest();
    
        //Do third request then wait for it to return
        yield return ThirdRequest();
    
        //Do fourth request then wait for it to return
        yield return FourthRequest();
    }
    

    EDIT:

    what if I only proceed to next coroutine if only I got success status? like www = new WWW(my_url); yield return www; if(!www.error) StartCoroutine(SecondRequest());

    In this case, you should use Action as a parameter in the coroutine functions:

    IEnumerator FirstRequest(Action<bool> reqStat)
    {
        www = new WWW(my_url);
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
            reqStat(false);
        else
            reqStat(true);
    }
    
    IEnumerator SecondRequest(Action<bool> reqStat)
    {
        www = new WWW(my_url);
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
            reqStat(false);
        else
            reqStat(true);
    }
    
    IEnumerator ThirdRequest(Action<bool> reqStat)
    {
        www = new WWW(my_url);
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
            reqStat(false);
        else
            reqStat(true);
    }
    
    IEnumerator FourthRequest(Action<bool> reqStat)
    {
        www = new WWW(my_url);
        yield return www;
        if (!string.IsNullOrEmpty(www.error))
            reqStat(false);
        else
            reqStat(true);
    }
    

    Usage:

    void Init()
    {
        StartCoroutine(doSequentialStuff());
    }
    
    IEnumerator doSequentialStuff()
    {
        bool reqStat = false;
    
        //Do first request then wait for it to return
        yield return FirstRequest((status) => { reqStat = status; });
    
        //Do second request then wait for it to return
        if (reqStat)
            yield return SecondRequest((status) => { reqStat = status; });
    
        //Do third request then wait for it to return
        if (reqStat)
            yield return ThirdRequest((status) => { reqStat = status; });
    
        //Do fourth request then wait for it to return
        if (reqStat)
            yield return FourthRequest((status) => { reqStat = status; });
    }
    
    0 讨论(0)
提交回复
热议问题