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方法得到对应表

相关推荐
yue00814 小时前
C# 求取整数的阶乘
java·开发语言·c#
黑咩狗夜.cm16 小时前
Aspose.word实现表格每页固定表头、最后一行填满整个页面
开发语言·c#·word
code bean16 小时前
【C#笔记】Newtonsoft.Json 中 `[JsonIgnore]` 的作用详解
笔记·c#·json
ccut 第一混17 小时前
用c# 制作一个扑克牌小游戏
开发语言·c#
IT老大哥18 小时前
局域网扫码枪/局域网二维码接收工具
c#·net
自由的好好干活18 小时前
C#桌面框架与Qt对比及选型(国产操作系统开发视角)
开发语言·qt·c#
The Sheep 202319 小时前
MicroService(Redis)
数据库·redis·c#
CodeCraft Studio20 小时前
Excel处理控件Aspose.Cells教程:如何使用C#在Excel中添加、编辑和更新切片器
ui·c#·excel·aspose·excel切片器·创建表格切片器
.NET修仙日记20 小时前
第四章:C# 面向对象编程详解:从类与对象到完整项目实践
开发语言·c#·.net·源码·教程·.net core
玖笙&21 小时前
✨WPF编程进阶【7.1】动画基础
c++·c#·wpf·visual studio