如何使用xlua实现协程,示例代码如下:

转载请注明出处:https://www.cnblogs.com/jietian331/p/10735773.html

 local unpack = unpack or table.unpack;
local function async_to_sync(async_func, callback_pos)
return function(...)
local _co = coroutine.running() or error ('this function must be run in coroutine')
local rets
local waiting = false
local function cb_func(...)
if waiting then
assert(coroutine.resume(_co, ...))
else
rets = {...}
end
end
local params = {...}
table.insert(params, callback_pos or (#params + ), cb_func)
async_func(unpack(params))
if rets == nil then
waiting = true
rets = {coroutine.yield()}
end return unpack(rets)
end
end -- 异步协程
local gameobject = CS.UnityEngine.GameObject('XLua_Coroutine_Runner');
CS.UnityEngine.Object.DontDestroyOnLoad(gameobject);
local csCoroutineRunner = gameobject:AddComponent(typeof(CS.XLuaCoroutineRunner));
local function AsyncYieldReturn(toYield, cb)
csCoroutineRunner:YieldAndCallback(toYield, cb)
end
local yieldReturn = async_to_sync(AsyncYieldReturn); -- 协程
local co = coroutine.create(function()
local url = CS.Common.LoadHelp.GetAssetBundleStreamingUrl("xxx.json");
local www = CS.UnityEngine.WWW(url);
yieldReturn(www); local text = www.text;
print(text);
end); coroutine.resume(co);

Unity XLua之协程的更多相关文章

  1. Unity中的协程(一)

    这篇文章很不错的问题,推荐阅读英文原版: Introduction to Coroutines Scripting with Coroutines   这篇文章转自:http://blog.csdn. ...

  2. Unity C#笔记 协程

    什么是协程 协同程序,在主程序运行的同时,开启另外一段逻辑处理,来协同当前程序的执行. 可能看了这段文字介绍还是有点模糊,其实可以用多线程来比较. 多线程 多线程,顾名思义,多条同时执行的线程. 最初 ...

  3. xlua 实现协程替换Unity中的协程

    C#中的协程: IEnumerator ShowSpiritInfo() { UIMessageMgr.ShowMsgWait(true); DestroyUIModelInfo(); bool is ...

  4. Unity中的协程是什么?

    什么是协程? 1.协程是一个分部执行,遇到条件(yield return 语句)会挂起,直到条件满足才会被唤醒继续执行后面的代码. 2.Unity在每一帧(Frame)都会去处理对象上的协程.Unit ...

  5. Unity中使用协程进行服务端数据验证手段

    近期在做项目中的个人中心的一些事情,用户头像上传,下载,本地缓存,二级缓存,压缩,这些都要做,麻雀虽小五脏俱全啊,也是写的浑浑噩噩的, 当我们在上传用户头像的时候,向服务端发送上传头像请求之前,一般都 ...

  6. 用C# 模拟实现unity里的协程

    注:需要了解C#的迭代器,不然很难理解. 之前面试有被问到unity协程的原理,以及撇开unity用纯C#去实现协程的方法.后来了解一下,确实可以的.趁这会有空,稍微总结一下. 还是结合代码说事吧: ...

  7. unity 自实现协程总结

    unity本人自实现了一个协程调用. 只是moveNext()的简单协程调用和封装,这个没什么好说的, 网上例子一大堆. 但使用的过程中遇到了几个问题. 1. 自己写的moveNext() 协程不能等 ...

  8. 【Unity笔记】协程Coroutine的简单优化

    一个最简单的协程,也至少需要2帧才能完成.第一帧走到yield return null停止,第二帧从此处接着执行完下面的操作.需求:如果缓存中存在某数据则直接使用,否则联网异步下载. private ...

  9. unity 中的协程

    //The coroutine will continue after all Update functionshave been calledon the next frame. ; //Conti ...

随机推荐

  1. 网络编程-Mysql-1、数据库的启动关闭,创建数据库,表等基本操作

    启动服务端:sudo service mysql start 关闭服务端:suodo service mysql stop 重启服务端:suodo service mysql restart 连接数据 ...

  2. PHPstorm远程连接侧边栏怎么打开,远程数据库侧边栏怎么打开

  3. MongDB增删改查

    增加 增加一条:db.th.insertOne({}) // 返回 _id 增加多条:db.th.insertMany([{},{},{}]) // 返回 _ids 针对Array增加操作: db.s ...

  4. debian包的补丁管理工具:quilt

    最近项目是改pam软件包,给里面添加一些功能.其中遇到了更改后,代码提交方式的问题.这里转载的文章介绍了使用quilt管理补丁的详细方法: 转自:http://blog.csdn.net/fmddlm ...

  5. [LeetCode] Kth Largest Element in a Stream 数据流中的第K大的元素

    Design a class to find the kth largest element in a stream. Note that it is the kth largest element ...

  6. 微信小程序采坑

    wx.request() complete回调函数执行时机问题 代码执行顺序有时候会严重影响用户体验:比如项目中请求数据时显示loading的图标,请求完成后不管失败还是成功都要把loading图标隐 ...

  7. js表单提交到后台对象接收

    $.extend({ StandardPost:function(url,args){ var form = $("<form method='post' target='_blank ...

  8. ubuntu16.04安装mrpt

    源码地址 https://github.com/MRPT/mrpt 安装教程 https://github.com/MRPT/mrpt/blob/master/README.md#32-build-f ...

  9. linux_grub resue模式恢复

    //20190417 今天中午装linux的电脑崩了…… 一开始还以为是什么硬件问题,搞了半天,是更新的时候吧grub启动项弄坏了 行吧,没人教咱就上网搜,搞一下午搞出来了 ============= ...

  10. init();

    <script language="javascript" type="text/javascript">window.onload = funct ...