XLua Coroutine协程
1.将XLua的util文件 放在自己的项目当中,在require 它XLua中的位置
移动到你项目的自定义的位置
2.在Lua层 封装XLua的 Corrutine 功能,代码如下
coroutine_cs={}--协程 利用了lua的 coroutine.wrap 创建协程 coroutine.yield挂起协程 coroutine.resume 重启协程
local util = require ('3rd/xlua/util')--Main是挂载在GameObjet的继承Mono的脚本 永不销毁
local cor_runner = GameObject.Find('GameManager'):GetComponent('Main')--开启协程
function coroutine_cs.start(...)
local action=util.cs_generator(...)
local cor=cor_runner:StartCoroutine(action)return cor
end
--挂起
function coroutine_cs.wait(seconds)
coroutine.yield(WaitForSeconds(seconds))--调起Unity 挂起若干秒
end
--停止协程
function coroutine_cs.stop(cor)if(cor)then
cor_runner:StopCoroutine(cor)
end
end
return coroutine_cs3.使用例子
require ('Common/Coroutine_CS')--封装代码的脚本
local a = coroutine_cs.start(function()print('coroutine a started')
coroutine_cs.wait(10)print('stop coroutine a after 10 seconds')
end)
local b = coroutine_cs.start(function()print('coroutine b started')
coroutine_cs.wait(3)
coroutine_cs.stop(a)--停止协程a可以注释 看停止协程是否生效
print('stop coroutine b after 3 seconds')
end)
页:
[1]