当前位置:网站首页>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)
边栏推荐
- 241. Design priority for operational expressions: DFS application questions
- 耐克如何常年霸榜第一名?最新財報答案來了
- Exploration and practice of inress in kubernetes
- 印象深刻的bug汇总(持续更新)
- Force button homepage introduction animation
- Can I open a securities account anywhere? Is it safe to open an account
- Seckill system 03 - redis cache and distributed lock
- Kafuka learning path (I) Kafuka installation and simple use
- Introduction to unittest framework and the first demo
- 妙啊!MarkBERT
猜你喜欢

Software project management 9.2 Software project configuration management process

深入理解 grpc part1

邻接矩阵无向图(一) - 基本概念与C语言

Neo4j 中文开发者月刊 - 202206期

How to understand the developed query statements

Shangtang entered the lifting period: the core management voluntarily banned and strengthened the company's long-term value confidence

2022/6/28学习总结

Matrix of numpy
![[Maui] add click events for label, image and other controls](/img/d6/7ac9632681c970ed99c9e4d3934ddc.jpg)
[Maui] add click events for label, image and other controls

自组织是管理者和成员的双向奔赴
随机推荐
ACLY与代谢性疾病
Istio, ebpf and rsocket Broker: in depth study of service grid
Acly and metabolic diseases
No statements may be issued when any streaming result sets are open and in use on a given connection
Skip the test cases to be executed in the unittest framework
Activity workflow engine
深入理解 grpc part1
Epoll introduction
想问问,证券开户有优惠吗手机开户是安全么?
kafuka学习之路(一)kafuka安装和简单使用
陈珙:微服务,它还那么纯粹吗?
Want to ask, is there a discount for opening a securities account? Is it safe to open a mobile account?
Computer graduation project asp Net hotel room management system VS development SQLSERVER database web structure c programming computer web page source code project
自组织是管理者和成员的双向奔赴
Nordic nrf52832 flash download M4 error
ES6 promise Usage Summary
证券账户销户后果 开户安全吗
Adjacency matrix undirected graph (I) - basic concepts and C language
JS日期格式化转换方法
用实际例子详细探究OpenCV的轮廓检测函数findContours(),彻底搞清每个参数、每种模式的真正作用与含义