tolua 学习笔记 插件的使用

参考文章:
http://blog.youkuaiyun.com/u010665359/article/details/50950989
http://blog.youkuaiyun.com/u010665359/article/details/51013433

tolua导入插件思路:其实框架里面都已经做好了扩展接口ToLuaExport.cs 里面的ProcessExtends函数。
注意:extendName = “ToLua_” + className.Replace(“.”, “”); 这是tolua约束好的格式以Tolua开头,也就是说插件导出来的函数名必须以Tolua开头,而且className也约束了这个函数将写入到哪个wrap。

extendType = Type.GetType(extendName + “, Assembly-CSharp-Editor”);后面就是type.GetMethods获取所有的方法进行读入,写入。

extendType.GetField(“AdditionNameSpace”); 这句大概就是加入的头 ,如果你需要加入头就在导出的插件函数这个字段写入头字段。比如你要导入的头是Dotween—>public static string AdditionNameSpace = “DG.Tweening”;

下面是操作步骤:

在LuaFramework/Editor下创建两个文件夹分别为LuaExtensions和Wrap,创建ToLuaFile.cs和ToLuaFileExport.cs放在LuaExtensions文件夹下。

ToLuaFile.cs

using System;

public static class ToLuaFile {

    public static Type[] exports = new Type[]
    {
        typeof(DG.Tweening.TweenSettingsExtensions),
        typeof(DG.Tweening.ShortcutExtensions),
        typeof(DG.Tweening.TweenExtensions),
    };
}

ToLuaFileExport.cs

using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Text;
using System.Collections.Generic;
using System;
using System.Reflection;
using System.IO;

public static class ToLuaFileExport
{
    [MenuItem("Lua/Export ToLuaExtendFile", false, 53)]
    public static void ExportToLuaExtendFile()
    {
        if (!Application.isPlaying)
        {
            EditorApplication.isPlaying = true;
        }

        Type[] list = ToLuaFile.exports;
        Dictionary<Type, List<MethodInfo>> dicTypeMethods = new Dictionary<Type, List<MethodInfo>>();
        for (int i = 0; i < list.Length; ++i)
        {
            Type type = list[i];
            List<MethodInfo> ltMethodInfo = new List<MethodInfo>();
            ltMethodInfo.AddRange(type.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static | BindingFlags.IgnoreCase | BindingFlags.DeclaredOnly));
            for (int j = 0; j < ltMethodInfo.Count; ++j)
            {
                MethodInfo method = ltMethodInfo[j];

                ParameterInfo[] parameterInfos = method.GetParameters();
                if (parameterInfos == null || parameterInfos.Length <= 0)
                    continue;

                Type parameterType = GetType(parameterInfos[0].ParameterType);
                if (parameterType.IsGenericParameter)
                    continue;

                if (dicTypeMethods.ContainsKey(parameterType))
                {
                    dicTypeMethods[parameterType].Add(method);
                }
                else
                {
                    List<MethodInfo> lt = new List<MethodInfo>();
                    lt.Add(method);
                    dicTypeMethods[parameterType] = lt;
                }
            }
        }

        foreach (KeyValuePair<Type, List<MethodInfo>> pair in
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值