当前位置:网站首页>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)
边栏推荐
- Neo4j Chinese developer monthly - issue 202206
- Redis启动与库进入
- Acly and metabolic diseases
- CAD如何设置标注小数位
- 小米手机解BL锁教程
- redis常识
- 我在中山,到哪里开户比较好?实际上网上开户安全么?
- 构建外部模块(Building External Modules)
- activity工作流引擎
- Shangtang entered the lifting period: the core management voluntarily banned and strengthened the company's long-term value confidence
猜你喜欢

Unittest框架中跳过要执行的测试用例

Deep understanding of grpc part1

深入理解 grpc part1

Matrix of numpy

CPU 上下文切换的机制和类型 (CPU Context Switch)
![[classic example] classic list questions @ list](/img/d8/a259e5f9d08eacbef31254d1bc3304.jpg)
[classic example] classic list questions @ list

Harbor webhook from principle to construction

陈珙:微服务,它还那么纯粹吗?

基于IMDB评论数据集的情感分析

Adjacency matrix undirected graph (I) - basic concepts and C language
随机推荐
S7-1500plc simulation
I'm in Zhongshan. Where is a better place to open an account? Is it actually safe to open an account online?
Force button homepage introduction animation
Learning summary on June 29, 2022
How to make the development of liquidity pledge mining system, case analysis and source code of DAPP defi NFT LP liquidity pledge mining system development
Computer graduation project asp Net attendance management system vs developing SQLSERVER database web structure c programming computer web page source code project
On recursion and Fibonacci sequence
我在中山,到哪里开户比较好?实际上网上开户安全么?
Share the method of how to preview PSD format and PSD file thumbnail plug-in [easy to understand]
[buuctf.reverse] 144_ [xman2018 qualifying]easyvm
Are the consequences of securities account cancellation safe
solo 可以通过 IPV6 访问吗?
妙啊!MarkBERT
Unittest 框架介绍及第一个demo
力扣首页简介动画
kafuka学习之路(一)kafuka安装和简单使用
树莓派4B安装tensorflow2.0[通俗易懂]
openinstall:微信小程序跳转H5配置业务域名教程
想问问,证券开户有优惠吗手机开户是安全么?
Prepare for the Blue Bridge Cup Day10__ PWM control light brightness