Unity tolua 把传递自定义对象参数

Posted jephone

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Unity tolua 把传递自定义对象参数相关的知识,希望对你有一定的参考价值。

首先创建一个类,用于当作一个对象。

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

以上是关于Unity tolua 把传递自定义对象参数的主要内容,如果未能解决你的问题,请参考以下文章

如何在我的自定义 JSP 标记中将整数值作为参数传递?

尝试传递自定义 oracle 类型对象映射时的名称模式无效

无法使用 Unity 在 Firebase-Analytics 中获取自定义参数

unity-tolua之Dispose释放引用

unity游戏自定义分辨率 教你如何自定义

shell 自定义带参数函数