当前位置:网站首页>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)
边栏推荐
- Unittest 框架介绍及第一个demo
- epoll介绍
- redis中value/list
- Redis启动与库进入
- Adjacency matrix undirected graph (I) - basic concepts and C language
- Test case writing specification in unittest framework and how to run test cases
- Abbirb120 industrial robot mechanical zero position
- 妙啊!MarkBERT
- CAD如何设置标注小数位
- Skip the test cases to be executed in the unittest framework
猜你喜欢

Computer graduation project asp Net attendance management system vs developing SQLSERVER database web structure c programming computer web page source code project

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

用实际例子详细探究OpenCV的轮廓检测函数findContours(),彻底搞清每个参数、每种模式的真正作用与含义

Harbor webhook from principle to construction

Use set_ Handler filters out specific SystemC wrapping & error messages

Comment Cao définit la décimale de dimension

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

Emotion analysis based on IMDB comment data set

深入理解 grpc part1

使用set_handler过滤掉特定的SystemC Wraning &Error Message
随机推荐
[Maui] add click events for label, image and other controls
Raspberry pie 4B installation tensorflow2.0[easy to understand]
ACLY与代谢性疾病
自定義 grpc 插件
8 best practices to protect your IAC security!
Value/sortedset in redis
Learning summary on June 29, 2022
The developer said, "this doesn't need to be tested, just return to the normal process". What about the testers?
Why must we move from Devops to bizdevops?
研发效能度量框架解读
Redis启动与库进入
证券账户随便哪里开都能使用吗 开户安全吗
Openinstall: wechat applet jump to H5 configuration service domain name tutorial
Value/hush in redis
Why does the JVM heap memory exceed 32g and pointer compression fail?
Seckill system 03 - redis cache and distributed lock
博途V15添加GSD文件
Epoll introduction
How does Nike dominate the list all the year round? Here comes the answer to the latest financial report
redis中value/list