c#调用lua之用LuaTable映射table(引用拷贝,效率低,有垃圾,不建议使用)

    科技2022-07-12  125

    注意:官方不建议用luatable,和luafunction去映射table,和table里的函数,因为效率低,有垃圾,这种是引用拷贝,改了值会反映在lua文件中,而且如果luatable不用了一定要释放dipose,不然产生垃圾

    test.lua

    --class --这样定义的表相当于是个自定义的类,有成员变量和成员函数 testClass={ testInt=1, testBool=true, testFloat=1.3, testString="13", testFun=function () -- body print("nihaoyahahahahha") end, }

    c#代码

    using System.Collections; using System.Collections.Generic; using UnityEngine; using XLua; public class CallLuaTable : MonoBehaviour { // Start is called before the first frame update void Start() { LuaMgr.GetInstance().Init(); LuaMgr.GetInstance().DoLuaFile("Main"); LuaTable table = LuaMgr.GetInstance().Global.Get<LuaTable>("testClass"); Debug.Log(table.Get<int>("testInt")); Debug.Log(table.Get<bool>("testBool")); Debug.Log(table.Get<string>("testString")); Debug.Log(table.Get<float>("testFloat")); table.Get<LuaFunction>("testFun").Call(); //设置值可以用set,如table.set("testInt",13); } }
    Processed: 0.015, SQL: 8