tolua 学习笔记 插件的使用

来源:互联网 发布:拼接屏软件 编辑:程序博客网 时间:2024/06/05 02:32

参考文章:
http://blog.csdn.net/u010665359/article/details/50950989
http://blog.csdn.net/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 dicTypeMethods)        {            if (pair.Key.IsGenericType)                continue;            SaveFile(pair.Key, pair.Value);        }        EditorApplication.isPlaying = false;        Debug.Log("Export ToLuaExtendFiles over");        AssetDatabase.Refresh();    }    static string ToLuaPath    {        get        {            return Application.dataPath + @"/LuaFramework/Editor/Wrap/";        }    }    static Type GetType(Type type)    {        if (type.IsGenericParameter)            return type.BaseType;        return type;    }    static string GetTypeStr(Type type)    {        Type trueType = GetType(type);        if (trueType == typeof(void))            return "void";        else            return ToLuaExport.GetTypeStr(trueType);    }    static void SaveFile(Type type, List<MethodInfo> ltMethodInfo)    {        string fileName = "ToLua_" + type.FullName.Replace(".", "_");        string path = ToLuaPath + fileName + ".cs";        if (File.Exists(path))            File.Delete(path);        List<string> ltUsing = new List<string>();        ltUsing.Add("System");        ltUsing.Add("UnityEngine");        using (StreamWriter textWriter = new StreamWriter(path, false, Encoding.UTF8))        {            StringBuilder usb = new StringBuilder();            foreach (string str in ltUsing)            {                usb.AppendFormat("using {0};\r\n", str);            }            usb.AppendLine();            usb.AppendFormat("public class {0}\r\n", fileName);            usb.AppendLine("{\r\n");            for (int i = 0; i < ltMethodInfo.Count; ++i)            {                MethodInfo m = ltMethodInfo[i];                string returnType = GetTypeStr(m.ReturnType);                usb.AppendFormat("\tpublic {0} {1}(", returnType, m.Name);                ParameterInfo[] parameterInfos = m.GetParameters();                for(int j = 1; j < parameterInfos.Length; ++j)                {                    ParameterInfo p = parameterInfos[j];                    usb.AppendFormat("{0} arg{1}", GetTypeStr(p.ParameterType), j);                    if (j < parameterInfos.Length - 1) usb.Append(", ");                }                usb.Append(")");                if (returnType == "void")                    usb.Append("\t{}\r\n");                else                    usb.Append("\t{ return default(" + returnType + "); }\r\n");            }            usb.AppendLine("}\r\n");            textWriter.Write(usb.ToString());            textWriter.Flush();            textWriter.Close();        }    }}

修改ToLuaExport.cs SaveFile 函数如下:

static void SaveFile(string file)    {                using (StreamWriter textWriter = new StreamWriter(file, false, Encoding.UTF8))        {                        StringBuilder usb = new StringBuilder();            usb.AppendLineEx("//this source code was auto-generated by tolua#, do not modify it");            foreach (string str in usingList)            {                usb.AppendFormat("using {0};\r\n", str);            }            usb.AppendLineEx("using LuaInterface;");            if (ambig == ObjAmbig.All)            {                usb.AppendLineEx("using Object = UnityEngine.Object;");            }            if (NeedDotweenNamespace())//给导出dotween的文件新增dotween的命名空间            {                usb.AppendLine("using DG.Tweening;");            }            usb.AppendLineEx();            textWriter.Write(usb.ToString());            textWriter.Write(sb.ToString());            textWriter.Flush();            textWriter.Close();        }      }

添加NeedDotweenNamespace函数:

static bool NeedDotweenNamespace()    {        //UnityEngine.Debug.Log(libClassName);        switch (libClassName)        {            case "Transform":            case "AudioSource":            case "Camera":            case "Component":            case "Light":            case "Material":            case "Sequence":            case "Tween":            case "Tweener":            case "Rigidbody":                return true;        }        return false;    }

修改ProcessExtends函数:

static void ProcessExtends(List<MethodInfo> list)    {        //这是tolua约束好的格式以Tolua_开头,也就是说插件导出来的函数名必须以Tolua开头,而且className也约束了这个函数将写入到哪个wrap        extendName = "ToLua_" + className.Replace(".", "_");        //后面就是type.GetMethods获取所有的方法读入,写入        extendType = Type.GetType(extendName + ", Assembly-CSharp-Editor");        List<string> removeMd = new List<string>();        if (extendType != null)        {            List<MethodInfo> list2 = new List<MethodInfo>();            list2.AddRange(extendType.GetMethods(BindingFlags.Instance | binding | BindingFlags.DeclaredOnly));            for (int i = list2.Count - 1; i >= 0; i--)            {                if (list2[i].Name.Contains("op_") || list2[i].Name.Contains("add_") || list2[i].Name.Contains("remove_"))                {                    if (!IsNeedOp(list2[i].Name))                    {                        //list2.RemoveAt(i);                        continue;                    }                }                string removeMdName = list2[i].Name;                if (!removeMd.Contains(removeMdName))//防止扩展函数里面重载函数被去除                {                    removeMd.Add(removeMdName);                    list.RemoveAll((md) => { return md.Name == removeMdName; });                }                //list.RemoveAll((md) => { return md.Name == list2[i].Name; });                if (!IsObsolete(list2[i]))                {                    list.Add(list2[i]);                }            }            //这句大概就是加入的头 ,如果你需要加入头就在导出的插件函数这个字段写入头字段。            //比如你要导入的头是Dotween—>public static string AdditionNameSpace = "DG.Tweening";            FieldInfo field = extendType.GetField("AdditionNameSpace");            if (field != null)            {                string str = field.GetValue(null) as string;                string[] spaces = str.Split(new char[] { ';' });                for (int i = 0; i < spaces.Length; i++)                {                    usingList.Add(spaces[i]);                }            }        }    }

好了,现在可以导入插件了,以DoTween为例,在CustomSettings.cs中添加如下:

_GT(typeof(DG.Tweening.AutoPlay)),        _GT(typeof(DG.Tweening.AxisConstraint)),        _GT(typeof(DG.Tweening.Ease)),        _GT(typeof(DG.Tweening.LogBehaviour)),        _GT(typeof(DG.Tweening.LoopType)),        _GT(typeof(DG.Tweening.PathMode)),        _GT(typeof(DG.Tweening.PathType)),        _GT(typeof(DG.Tweening.RotateMode)),        _GT(typeof(DG.Tweening.ScrambleMode)),        _GT(typeof(DG.Tweening.TweenType)),        _GT(typeof(DG.Tweening.UpdateType)),        _GT(typeof(DG.Tweening.DOTween)),        _GT(typeof(DG.Tweening.DOVirtual)),        _GT(typeof(DG.Tweening.EaseFactory)),        _GT(typeof(DG.Tweening.Tweener)),        _GT(typeof(DG.Tweening.Tween)),        _GT(typeof(DG.Tweening.Sequence)),        _GT(typeof(DG.Tweening.TweenParams)),        _GT(typeof(DG.Tweening.DOTweenAnimation)),        _GT(typeof(DG.Tweening.DOTweenPath)),        _GT(typeof(DG.Tweening.DOTweenVisualManager)),        _GT(typeof(DG.Tweening.Core.ABSSequentiable)),        //注意遇到泛型的要指明类型导出。        _GT(typeof(DG.Tweening.Core.TweenerCore<Vector3, Vector3, DG.Tweening.Plugins.Options.VectorOptions>)).SetWrapName("TweenerCoreV3V3VO").SetLibName("TweenerCoreV3V3VO"),

好了重新Wrap。

测试:

DoTweenTest.cs

public class DoTweenTest : MonoBehaviour {    private LuaState lua;    void Start ()    {        lua = new LuaState();        lua.Start();        LuaBinder.Bind(lua);        lua.DoFile("Main.lua");        LuaFunction main = lua.GetFunction("Main");        main.Call();        main.Dispose();        main = null;    }}

Main.lua

--主入口函数。从这里开始lua逻辑require "UnityEngine/Vector3"require "Common/define"function Main()                     --AddEvent();    DoTween()endfunction DoTween()    local go = GameObject.Find("Cube");    local sequence =  DG.Tweening.DOTween.Sequence();    sequence:Append(go.transform:DOLocalMove(Vector3.New(10,0,0),3,false))    :SetEase(DG.Tweening.Ease.Linear);    sequence:OnComplete(Complete);    --sequence:SetLoops(-1,DG.Tweening.LoopType.Yoyo);endfunction Complete()    print("222");end

这边提一下:

sequence:Append(go.transform:DOLocalMove(Vector3.New(10,0,0),3,false))

在c#中DOLocalMove的第三个参数可以省略,但在lua是不可以省略的,注意一下,是不是所有c#可省的,在lua中都不可省的呢?有待研究。

笔者也是刚刚接触LuaFramework,博文只是作为学习笔记与大家分享,希望能与大家共同进步。


每天进步一点点。

0 0
原创粉丝点击