How to continue the function only after the coroutine completes?

前端 未结 2 1894
我寻月下人不归
我寻月下人不归 2021-01-27 16:24
void Generate()
{
    StartCoroutine(FallDelayCoroutine());
    print(\"time3- \" + Time.time);
}

IEnumerator FallDelayCoroutine()
{     
    print(\"time1- \"+ Time.ti         


        
2条回答
  •  情歌与酒
    2021-01-27 17:15

    As alternative to the other answer using two Coroutines you could also add a callback like e.g.

    IEnumerator FallDelayCoroutine(Action whenDone)
    {     
        print("time1- "+ Time.time);
        yield return new WaitForSeconds(3f);
        print("time2- " + Time.time);
    
        whenDone?.Invoke();
    }
    

    and call it either using e.g. a lambda expression

    void Generate()
    {
        StartCoroutine(FallDelayCoroutine(() => {
            print("time3- " + Time.time);
        }));  
    }
    

    or with a method

    void Generate()
    {
        StartCoroutine(FallDelayCoroutine(OnFallDelayDone));  
    }
    
    private void OnFallDelayDone()
    {
        print("time3- " + Time.time);
    }
    

提交回复
热议问题