解决Unity协程无法同步返回的问题
Unity的协程是轻量的异步解决方案,但是每调用一次yield就必须等下一帧才能继续,这一点带来了很多约束。
比如如下代码:
void OnEnable()
{
StartCoroutine(_Do());
} IEnumerator _Do()
{
Debug.Log("[A]Frame " + Time.frameCount);
yield return null;
Debug.Log("[B]Frame " + Time.frameCount);
}
当然,也会想到用一些Trick欺骗过去
IEnumerator Start()
{
Debug.Log("[0]frame: " + Time.frameCount); yield return Foo1(); yield return Foo2();
} IEnumerator Foo1()
{
Debug.Log("[1]frame: " + Time.frameCount);
if (Time.time < )//always false
yield return null;
Debug.Log("[2]frame: " + Time.frameCount);
} IEnumerator Foo2()
{
Debug.Log("[3]frame: " + Time.frameCount); yield return null;
}
可是编译器并不吃这一套
那么解决方法也很简单,就是用迭代器再封装一层。
并把yield return true作为非异步返回的标记:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System; public class CoroutineTest : MonoBehaviour
{
void OnEnable()
{
StartCoroutine(ToFixedCoroutine(_Do()));
} IEnumerator _Do()
{
Debug.Log("[A]Frame " + Time.frameCount);
yield return true;
Debug.Log("[B]Frame " + Time.frameCount);
} public static IEnumerator ToFixedCoroutine(IEnumerator enumerator)
{
var parentsStack = new Stack<IEnumerator>();
var currentEnumerator = enumerator; parentsStack.Push(currentEnumerator); while (parentsStack.Count > )
{
currentEnumerator = parentsStack.Pop(); while (currentEnumerator.MoveNext())
{
var subEnumerator = currentEnumerator.Current as IEnumerator;
if (subEnumerator != null)
{
parentsStack.Push(currentEnumerator);
currentEnumerator = subEnumerator;
}
else
{
if (currentEnumerator.Current is bool && (bool)currentEnumerator.Current) continue;
yield return currentEnumerator.Current;
}
}
}
}
}
这样就可以同步返回了
ToFixedCoroutine函数经过一些嵌套的测试,使用起来还算稳定。
解决Unity协程无法同步返回的问题的更多相关文章
- Unity协程(Coroutine)原理深入剖析
Unity协程(Coroutine)原理深入剖析 By D.S.Qiu 尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com 其实协程并没有那么复杂,网上很多地方都说是多 ...
- Unity协程(Coroutine)原理深入剖析再续
Unity协程(Coroutine)原理深入剖析再续 By D.S.Qiu 尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com 前面已经介绍过对协程(Coroutine ...
- Unity协程(Coroutine)原理深入剖析(转载)
记得去年6月份刚开始实习的时候,当时要我写网络层的结构,用到了协程,当时有点懵,完全不知道Unity协程的执行机制是怎么样的,只是知道函数的返回值是IEnumerator类型,函数中使用yield r ...
- Unity协程使用经验
[Unity协程使用经验] 1.协程的好处是,异步操作发起的地方和结束的地方可以统一在一个方法,这样就不用引入额外的成员变量来进行状态同步. 2.在一个协程中,StartCoroutine()和 yi ...
- 【转】Unity协程(Coroutine)原理深入剖析
Unity协程(Coroutine)原理深入剖析 By D.S.Qiu 尊重他人的劳动,支持原创,转载请注明出处:http.dsqiu.iteye.com 记得去年6月份刚开始实习的时候,当时要我写网 ...
- 聊一聊Unity协程背后的实现原理
Unity开发不可避免的要用到协程(Coroutine),协程同步代码做异步任务的特性使程序员摆脱了曾经异步操作加回调的编码方式,使代码逻辑更加连贯易读.然而在惊讶于协程的好用与神奇的同时,因为不清楚 ...
- unity协程coroutine浅析
转载请标明出处:http://www.cnblogs.com/zblade/ 一.序言 在unity的游戏开发中,对于异步操作,有一个避免不了的操作: 协程,以前一直理解的懵懵懂懂,最近认真充电了一下 ...
- 深入浅出!从语义角度分析隐藏在Unity协程背后的原理
Unity的协程使用起来比较方便,但是由于其封装和隐藏了太多细节,使其看起来比较神秘.比如协程是否是真正的异步执行?协程与线程到底是什么关系?本文将从语义角度来分析隐藏在协程背后的原理,并使用C++来 ...
- Unity 协程使用指南
0x00 前言 在使用Unity的过程中,对协程仅仅知道怎样使用,但并不知道协程的内部机理,对于自己不清楚的部分就像一块大石压力心里.让自己感觉到担忧和不适. 这篇文章一探到底,彻底揭开协程的面纱,让 ...
随机推荐
- JAVA & Android 等待线程池内任务全部完成后退出
void shutdownAndAwaitTermination(ExecutorService pool) { pool.shutdown(); // Disable new tasks from ...
- cocos2d-js Shader系列2:在cc.Sprite上使用Shader(黑白、灰度、造旧效果)
在Sprite中使用Shader做特殊的颜色处理比较简单,只需要把Shader程序绑定到Sprite上即可: sprite.shaderProgram = alphaTestShader; Cocos ...
- Reading CheckBoxes and Radio Buttons
Input tags with the type attribute checkbox can be grouped like radio buttons so that several checkb ...
- 【linux】linux 环境下 安装禅道(转载) -- 跟web服务器无关,无视apache、nginx!!!
下载地址:http://www.zentao.net/download/zentao10.0.beta-80076.html 参考文章 链接 :https://blog.csdn.net/xinxin ...
- ios中第三方库归结
1:uiscrollview 折叠 展开中不包含tablecell. 展开列表效果 Collapse Click () https://github.com/bennyguitar/Collapse ...
- Wget用法、参数解释
wget功能的强大就不用多说了,在高手手里,它就像是个无往不利的杀人利器,下面是转载的一篇Wget用法.参数解释的比较好的一个文章,当然最好的老师还是man wget 是一个从网络上自动下载文件的自由 ...
- 清除li内a标签的float=left实现a标签在li内居中显示(ul内li不居中显示)
写在前面: 修改cnblogs主页面菜单显示问题. 问题描述:在给主菜单添加hover样式后发现菜单内容并未居中.见图1. 网上搜索到资料其中一篇讲的可以说简明扼要了,也是伸手党的福利(点我查看原文) ...
- Linux端口命令
一.开启端口 1.命令行方式 1.开放端口命令: /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT 2.保存:/etc/rc.d/init.d ...
- Using PHP as a Spring MVC View via Quercus(转)
原贴: http://blog.caucho.com/2009/04/14/using-php-as-a-spring-mvc-view-via-quercus/ This week, I’ve be ...
- HDU 1556 Color the ball (数状数组)
Color the ball Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...