标签:integer style sse awr obj sea unit turn 复制
首先创建一个类,用于当作一个对象。
public class LuaTestA { public int a = 10; }
接着创建第二个类
public class LuaTestB { public int b = 10; public LuaTestA luaTestA; }
对LuaTestA进行Lua绑定。
using LuaInterface; using System; public class LuaTestAWrap { public static Regist(LuaState L) { L.BeginClass(typeof(LuaTestA),typeof(System.Object)); ///LuaTestA中的变量a L.RegVar("a",get_a,set_a); L.EndClass(); } [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] static int get_a(IntPtr L) { object o = null; try { o = ToLua.ToObject(L,1); LuaTestA obj = (LuaTestA)o; int ret = obj.a; LuaDll.lua_pushinteger(L,ret); return 1; } catch (Exception e) { return LuaDLL.toluaL_exception(L, e, o, "attempt to index text on a nil value"); } } [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] static int set_a(IntPtr L) { object o = null; try { o = ToLua.ToObject(L,1); LuaTestA obj = (LuaTestA)o; int arg0 = ToLua.CheckValue<int>(L,2); obj.a = arg0; return 1; } catch (Exception e) { return LuaDLL.toluaL_exception(L, e, o, "attempt to index text on a nil value"); } } }
对LuaTestB进行Lua绑定。
using LuaInterface; using System; public class LuaTestBWrap { public static Regist(LuaState L) { L.BeginClass(typeof(LuaTestB),typeof(System.Object)); ///LuaTestB中的变量b L.RegVar("b",get_b,set_b); L.RegVar("luaTestA",get_LuaTestA,set_LuaTestA); L.EndClass(); } [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] static int get_b(IntPtr L) { object o = null; try { o = ToLua.ToObject(L,1); LuaTestA obj = (LuaTestA)o; int ret = obj.a; LuaDll.lua_pushinteger(L,ret); return 1; } catch (Exception e) { return LuaDLL.toluaL_exception(L, e, o, "attempt to index text on a nil value"); } } [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] static int set_b(IntPtr L) { object o = null; try { o = ToLua.ToObject(L,1); LuaTestA obj = (LuaTestA)o; int arg0 = ToLua.CheckValue<int>(L,2); obj.a = arg0; return 1; } catch (Exception e) { return LuaDLL.toluaL_exception(L, e, o, "attempt to index text on a nil value"); } } [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] stati int get_LuaTestA(IntPtr L) { object o = null; try { o = ToLua.ToObject(L,1); LuaTestB obj = (LuaTestB)o; LuaTestA ret obj.luaTestA; ToLua.PushSealed(L,ret); return 1; } catch (Exception e) { return LuaDLL.toluaL_exception(L, e, o, "attempt to index text on a nil value"); } } [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] static int set_b(IntPtr L) { object o = null; try { o = ToLua.ToObject(L,1); LuaTestA obj = (LuaTestA)o; int arg0 = ToLua.CheckValue<int>(L,2); obj.a = arg0; return 1; } catch (Exception e) { return LuaDLL.toluaL_exception(L, e, o, "attempt to index text on a nil value"); } } [MonoPInvokeCallbackAttribute(typeof(LuaCSFunction))] stati int set_LuaTestA(IntPtr L) { object o = null; try { o = ToLua.ToObject(L,1); LuaTestB obj = (LuaTestB)o; LuaTestA arg0 = (LuaTestA)ToLua.CheckObject(L,2,typeof(LuaTestA)); obj.luaTestA = arg0; return 1; } catch (Exception e) { return LuaDLL.toluaL_exception(L, e, o, "attempt to index text on a nil value"); } } }
Mono脚本
using LuaInterface; using UnityEngine; public class Test : MonoBehaviour { LuaState lua; LuaTestB[] luaBs; LuaFunction luaFun; private void Start() { luaBs = new LuaTestB[2]; luaBs[0] = new LuaTestB() { luaTestA = new LuaTestA() }; luaBs[1] = new LuaTestB() { luaTestA = new LuaTestA() }; lua = new LuaState(); lua.Start(); LuaBinder.Bind(lua); Blind(lua); lua.DoFile(Application.streamingAssetsPath + "/testlua.lua"); luaFun = lua.GetFunction("test"); luaFun.BeginPCall(); luaFun.Push(luaBs); luaFun.PCall(); luaFun.EndPCall(); } private void OnApplicationQuit() { if (luaFun!=null) { luaFun.Dispose(); luaFun = null; } lua.Dispose(); } void Blind(LuaState lua) { lua.BeginModule(null); LuaTestAWrap.Register(lua); LuaTestBWrap.Register(lua); lua.EndModule(); } }
lua脚本:文件名为test.lua,放到streamingassets文件夹下
function test( val ) -- body print(val[0].b) print(val[1].luaTestA.a) end
标签:integer style sse awr obj sea unit turn 复制
原文地址:https://www.cnblogs.com/jephone/p/11160775.html