等待或等待协程完成

编程入门 行业动态 更新时间:2024-10-26 13:31:36
本文介绍了等待或等待协程完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在下面的示例中,如何在运行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).

更多推荐

等待或等待协程完成

本文发布于:2023-11-23 11:12:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621242.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!