Lua热更学习--使用toLua中的协程
  1wMBnbixARwC 2024年05月17日 47 0

[6] C#访问调table类中的成员变量和函数

访问table中的变量和函数

lua中可以使用table作为class,因此对table中的函数访问调用是必要的根据前面对table访问和function的获取调用,这里尝试获取调用。

依然是如此,此种调用方式获取到的table中的函数是引用拷贝。

Main.lua脚本新增内容

CStudent = {
	_name = "TonyChang",
	_id = "202499990101",
	_sex = "male",
	CStudent = function()
		print("table中的函数")
	end
}  

测试脚本中的调用内容:

//--------------------------------获取类类型table
LuaTable CStudent = CallLuaManager.Instance().LuaState.GetTable("CStudent");
//执行构造函数
CStudent.GetLuaFunction("CStudent").Call("TonyChang","男");
//打印结果
Debug.Log(CStudent["_name"]+" ," + CStudent["_id"] +" ," +CStudent["_sex"]);

使用toLua中的协程

lua中不支持协程,使用toLua中提供的协程方式来使用协程。

在使用协程之前,需要在管理类中添加LuaLooper组件,并将其LuaState与外部使用执行的LuaState虚拟机绑定。

LuaLooper luaLooper = gameObject.AddComponent<LuaLooper>();
// Debug.Log(gameObject.name);
luaLooper.luaState = _luaState;

Main.lua中的协程:

--使用toLua中提供的协程
--制作计时器
function Timer()  
	local t = 1
	while t < 20 do
		t = t + 1
		coroutine.wait(1)
		print(t)
	end
	StopTimer()
end
local coroutlineTimer = nil

function StartTimer()
	print("run")
	--开始协程时候传入类型为函数
	coroutlineTimer = coroutine.start(Timer)
end

function StopTimer()
	--传入要结束的协程
	coroutine.stop(coroutlineTimer)
end
 

我们在C#测试脚本中开启协程:

 //----------------------------------开始计时器
 LuaFunction startTimer = CallLuaManager.Instance().LuaState.GetFunction("StartTimer");
 startTimer.Call();
 startTimer.Dispose();

image-20240512112431495

当然也可以传入参数设置计时时长:

image-20240512112758804

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年05月17日 0

暂无评论

推荐阅读
  oXKBKZoQY2lx   2024年05月17日   54   0   0 游戏开发
  1wMBnbixARwC   2024年05月17日   50   0   0 游戏开发
  1wMBnbixARwC   2024年05月08日   77   0   0 游戏开发
1wMBnbixARwC