boxmoe_header_banner_img

菜就多练喵

文章导读

[Unity][XLua]热更新12——用C#获取Lua全局变量


avatar
Ib_Mccf 2026年3月5日 13

Main.lua

print("载入Main.lua")
--Unity中写lua脚本
--Xlua已经帮我们处理了
--执行lua脚本,都会自动进入我们的自动重定向方法寻找文件
require("Test")

Test.lua

print("载入Test.lua")
testNumber = 1
testFloat = 1.14514
testString = "Hello, World!"
testBool = false
--无法通过c#直接获取local变量
local testLocal = 10

C#

LuaMgr.GetInstance().Init();
LuaMgr.GetInstance().DoLuaFile("Main");//执行Main.lua脚本,在Main.lua中执行Test.lua
//获取lua中的全局变量,无法获取本地变量local
LuaTable global = LuaMgr.GetInstance().Global;
int testNumber = global.Get<int>("testNumber");
float testFloat = global.Get<float>("testFloat");
double testDouble = global.Get<double>("testFloat");
bool testBool = global.Get<bool>("testBool");
string testString = global.Get<string>("testString");
Debug.Log("testNumber:" + testNumber);
Debug.Log("testFloat:" + testFloat);
Debug.Log("testDouble:" + testDouble);
Debug.Log("testBool:" + testBool);
Debug.Log("testString:" + testString);
//更改lua全局变量的值
global.Set("testNumber", 5);
Debug.Log("更改后的testNumber:" + global.Get<int>("testNumber"));


评论(0)

查看评论列表

暂无评论


发表评论

表情 颜文字

插入代码