Unity tolua 把传递自定义对象参数
2020-12-13 05:58
标签:integer style sse awr obj sea unit turn 复制 首先创建一个类,用于当作一个对象。 接着创建第二个类 对LuaTestA进行Lua绑定。 对LuaTestB进行Lua绑定。 Mono脚本 lua脚本:文件名为test.lua,放到streamingassets文件夹下 Unity tolua 把传递自定义对象参数 标签:integer style sse awr obj sea unit turn 复制 原文地址:https://www.cnblogs.com/jephone/p/11160775.htmlpublic class LuaTestA
{
public int a = 10;
}
public class LuaTestB
{
public int b = 10;
public LuaTestA luaTestA;
}
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
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
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();
}
}
function test( val )
-- body
print(val[0].b)
print(val[1].luaTestA.a)
end
上一篇:python 之 函数
下一篇:计算机常识--Windows篇