C#内映射lua表

都是通过同一个方法得到的

例如得到List

复制代码
List<int> list = LuaMgr.GetInstance().Global.Get<List<int>>("testList");

只要把Get的泛型换成对应的类型即可

得到Dictionnary

复制代码
Dictionary<string, int> dic2 = LuaMgr.GetInstance().Global.Get<Dictionary<string, int>>("testDic");

得到类

类里面的成员都必须是公有public的,而且成员名字要和lua脚本内的类相同

cs 复制代码
public class CallLuaClass
{
    //在这个类中去声明成员变量
    //名字一定要和 Lua那边的一样
    //公共 私有和保护 没办法赋值
    //这个自定义中的 变量 可以更多也可以更少
    //如果变量比 lua中的少 就会忽略它
    //如果变量比 lua中的多 不会赋值 也会忽略
    public int testInt;
    public bool testBool;
    public float testFloat;
    public float testString;
    public UnityAction testFun;

    public CallLuaInClass testInClass;
}

public class CallLuaInClass
{
    public int testInInt;
}

lua的类

Lua 复制代码
CallLuaClas = {
	testInt = 1,
	testBool = true,
	testFloat = 1.1,
	testString = "1",
    
    function testFun()
    	print("function")
    end,

    testInClass = {
    	testInInt = 5,
    }
}

映射类代码

cs 复制代码
CallLuaClass obj = LuaMgr.GetInstance().Global.Get<CallLuaClass>("testClas");

万能类LuaTable装表(不建议使用)

cs 复制代码
void Start()
    {
        LuaMgr.GetInstance().Init();
        LuaMgr.GetInstance().DoLuaFile("Main");

        //不建议使用LuaTable和LuaFunction 效率低
        //引用对象
        LuaTable table = LuaMgr.GetInstance().Global.Get<LuaTable>("testClas");
        Debug.Log(table.Get<int>("testInt"));
        Debug.Log(table.Get<bool>("testBool"));
        Debug.Log(table.Get<float>("testFloat"));
        Debug.Log(table.Get<string>("testString"));

        table.Get<LuaFunction>("testFun").Call();

        //改  引用
        table.Set("testInt", 55);

        //使用完要销毁
        table.Dispose();
    }

备注*

LuaMgr是自己写的一个Xlua管理器,他是单例模式管理器,里面定义了luaEnv并实例化。GetInstance方法是得到这个LuaMgr单例类的,Global返回了luaEnv内的_G表,其中_G自带Get方法得到对应表

相关推荐
xiaoshuaishuai81 小时前
C# 接入 OpenClaw
windows·visualstudio·c#
gihigo19987 小时前
嵌入式幼儿园刷卡系统 (C#实现)
c#
qq_454245037 小时前
通用引用管理框架
数据结构·架构·c#
aq55356007 小时前
三大编程语言深度对比:C# vs 易语言 vs 汇编
开发语言·汇编·c#
光泽雨7 小时前
c# 文件编译的过程
开发语言·c#
zxy28472253017 小时前
使用正运动的仿真软件C#
c#·仿真·运动控制·正运动·无硬件
三省持敬8 小时前
异步并发的“流量警察”:在C#中使用SemaphoreSlim进行并发控制的最佳实践
c#
唐青枫9 小时前
C#.NET IL 中间码 深入解析:从 C# 编译结果到 CLR 执行链路
c#·.net
xiaoshuaishuai810 小时前
C# 方言识别
开发语言·windows·c#
波波00711 小时前
写出稳定C#系统的关键:不可变性思想解析
开发语言·c#·wpf