等待或等待协程完成 [英] Hold or Wait while Coroutine finishes

查看:538
本文介绍了等待或等待协程完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


在下面的示例中,如何在运行DoLast()之前先完成FinishFirst()并同时保留"public void StartPage()"签名?


In the example below, how can I get FinishFirst() to complete first before running DoLast(), while still retaining the 'public void StartPage()' signature?


我试图避免使"StartPage()"返回IEnumerator,因为这将迫使我在界面中对其进行更改.如果我的StartPage()接口同时支持IEnumerator和Void而不需要同时实现这两者,那就太好了.

I'm trying to avoid making "StartPage()" return an IEnumerator as that would force me to change it in the interface. It would be great if my Interface for StartPage() supported both IEnumerator and Void without needing to implement both.

public void StartPage()
{
    print("in StartPage()");
    StartCoroutine(FinishFirst(5.0f));
    DoLast();
    print("done");

}    

IEnumerator FinishFirst(float waitTime)
{
    print("in FinishFirst");        
    yield return WaitForSeconds(waitTime);
    print("leave FinishFirst");
}    

void DoLast()
{
    print("do after everything is finished");
}

推荐答案

我知道这是一个老问题,但是如果我正确理解了这个问题,那么类似的方法就可以了. DoLast()将在协同例程的结尾运行.

I know this is an old question, but if I understand the question correctly, something like this works. DoLast() will run at the end of the co-routine.

IEnumerator FinishFirst(float waitTime)
{
    print("in FinishFirst");        
    yield return WaitForSeconds(waitTime);
    print("leave FinishFirst");
    DoLast();
}  

如果在例程运行时创建了另一个例程(主要用于键入文本对话框,其中文本可能随时更改),我会在协例程中使用很多类似的代码来清理并终止该例程.

I use a lot of similar code inside co-routines to clean and kill the routine if another one was created when this one is running (mainly for typed-text dialog boxes where the text might change at any time).

这篇关于等待或等待协程完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆