unity tolua 学习笔记

本文介绍如何使用tolua将自定义的C#类MyTestObject转换为Lua可调用的形式,并演示了如何在Lua脚本中调用C#类的方法及设置自定义委托。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

toLua学习笔记
tolua版本1.0.7
1.生成wrap文件
 创建自定义的类MyTestObject  在Editor/Custom/CustomSettings 的customTypeList 中添加自定义的类型
     _GT(typeof(QualitySettings)),
        _GT(typeof(RenderSettings)),                                                   
        _GT(typeof(BlendWeights)),           
        _GT(typeof(RenderTexture)),
        _GT(typeof(Resources)),   
        _GT(typeof(MyTestObject)),  
 点击unity菜单栏中的lua/gen lua Wrap + binder 运行后可以再Source/Generate中发现MytestObjectWrap文件 并且luabinder中也有注册信息
 调用方法
   LuaState lua = new LuaState();
        lua.Start();
        string hello =
            @"                
                MyTestObject.PrintTest()                                 
            ";
        LuaBinder.Bind(lua); //调用将c#中的类传给lua
        lua.DoString(hello);
        lua.CheckTop();
        lua.Dispose();
        lua = null;




       tolua添加委托


       在customseting中添加自定义的委托类型
           //附加导出委托类型(在导出委托时, customTypeList 中牵扯的委托类型都会导出, 无需写在这里)
    public static DelegateType[] customDelegateList = 
    {        
        _DT(typeof(Action)),                
        _DT(typeof(UnityEngine.Events.UnityAction)),
        _DT(typeof(System.Predicate<int>)),
        _DT(typeof(System.Action<int>)),
        _DT(typeof(System.Comparison<int>)),
        _DT(typeof(System.Func<int, int>)),
        _DT(typeof(MyTongDelegate.PrintTestDelegate)),
    };


在下面的类型中 添加自定义的类
  public static BindType[] customTypeList =
    {                
        //------------------------为例子导出--------------------------------
        //_GT(typeof(TestEventListener)),
        //_GT(typeof(TestProtol)),
        //_GT(typeof(TestAccount)),
        //_GT(typeof(Dictionary<int, TestAccount>)).SetLibName("AccountMap"),
        //_GT(typeof(KeyValuePair<int, TestAccount>)),
        //_GT(typeof(Dictionary<int, TestAccount>.KeyCollection)),
        //_GT(typeof(Dictionary<int, TestAccount>.ValueCollection)),
        //_GT(typeof(TestExport)),
        //_GT(typeof(TestExport.Space)),
        //-------------------------------------------------------------------        
                        
        _GT(typeof(Debugger)).SetNameSpace(null),          
        _GT(typeof(MyTongDelegate)),


lua中  直接通过类访问代理 通过= 或者 +绑定代理
 function luaDele(listener)
            listener.myDele =  print1
        end


   整体代码


   using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using LuaInterface;


public class MyTongDelegate : MonoBehaviour {


    public delegate void PrintTestDelegate();
    public PrintTestDelegate myDele = delegate { };
    // Use this for initialization
    void Start () {
        string luaStr = @"
        function print1()
            print('print from lua')
        end


        function luaDele(listener)
            listener.myDele =  print1
        end
";
        LuaState lua = new LuaState();
        lua.Start();
        LuaBinder.Bind(lua);
        DelegateFactory.Init();
        lua.DoString(luaStr);
        LuaFunction addClick = lua.GetFunction("luaDele");
        addClick.Call<MyTongDelegate>(this);


        myDele();
    }

// Update is called once per frame
void Update () {

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值