当前位置:网站首页>Unity XLua 协程封装
Unity XLua 协程封装
2022-07-01 11:51:00 【帅_shuai_】
Unity XLua 协程封装
- 依附Unity中的MonoBehaviour对象来开启协程
- MonoBehaviour对象只需要是空的脚本,主要来进行协程的开启
- 外界使用时只需要对该协程对象进行操作
1.封装cs_coroutine
local util = require("xlua.util")
local obj = CS.UnityEngine.GameObject("Coroutine")
CS.UnityEngine.Object.DontDestroyOnLoad(obj)
[email protected] UnityEngine.MonoBehaviour
local mono = obj:AddComponent(typeof(CS.TestMono))
return{
start = function(...)
return mono:StartCoroutine(util.cs_generator(...))
end,
stop = function(co)
mono:StopCoroutine(co)
end,
stopAll = function()
mono:StopAllCoroutines()
end
}
2.使用cs_coroutine(1)
local cs_coroutine = require("Lesson23")
local co_a = cs_coroutine.start(function()
print("coroutine a started")
coroutine.yield(cs_coroutine.start(function()
print("coroutine b stated inside coroutine a")
coroutine.yield(CS.UnityEngine.WaitForSeconds(1))
print("i am coroutine b")
end))
print("coroutine b finish")
while true do
coroutine.yield(CS.UnityEngine.WaitForSeconds(1))
print("i am coroutine a")
end
end)
cs_coroutine.start(function()
print("stop coroutine a after 5 seconds")
coroutine.yield(CS.UnityEngine.WaitForSeconds(5))
cs_coroutine.stop(co_a)
print("coroutine a stop")
end)
3.使用cs_coroutine(2)
- 满足条件停止自身协程
local cs_coroutine = require("Lesson23")
co = cs_coroutine.start(function()
local a = 0
while true do
print(a)
a = a + 1
if a >= 5 then
cs_coroutine.stop(co)
co = nil
for i, v in pairs(_G) do
print(i,v)
end
end
coroutine.yield(CS.UnityEngine.WaitForSeconds(0.2))
end
end)
边栏推荐
猜你喜欢

Dameng data rushes to the scientific innovation board: it plans to raise 2.4 billion yuan. Feng Yucai was once a professor of Huake

Compile and debug net6 source code

Istio, ebpf and rsocket Broker: in depth study of service grid

C summary of knowledge points 1

Oneconnect plans to be listed in Hong Kong on July 4: a loss of nearly 3 billion in two years, with a market capitalization evaporation of more than 90%

Why must we move from Devops to bizdevops?

Unittest 框架介绍及第一个demo

Istio、eBPF 和 RSocket Broker:深入研究服务网格

Deep understanding of grpc part1

图的理论基础
随机推荐
241. 为运算表达式设计优先级 : DFS 运用题
证券账户销户后果 开户安全吗
Learning summary on June 28, 2022
Software project management 9.2 Software project configuration management process
ES6 Promise用法小结
Value/list in redis
redis中value/list
2022/6/28学习总结
S7-1500plc simulation
印象深刻的bug汇总(持续更新)
Istio、eBPF 和 RSocket Broker:深入研究服务网格
Acly and metabolic diseases
Goldfish rhca memoirs: do447 uses ansible to communicate with API -- using ansible tower API to start jobs
Nordic nrf52832 flash 下载M4错误
研发效能度量框架解读
S7-1500PLC仿真
Explore the contour detection function findcontours() of OpenCV in detail with practical examples, and thoroughly understand the real role and meaning of each parameter and mode
Binary stack (I) - principle and C implementation
耐克如何常年霸榜第一名?最新财报答案来了
Botu V15 add GSD file