tolua 學習筆記 插件的使用

參考文章:
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()
end

function 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);
end

function Complete()
    print("222");
end

這邊提一下:

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

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

筆者也是剛剛接觸LuaFramework,博文只是作爲學習筆記與大家分享,希望能與大家共同進步。


每天進步一點點。

發佈了40 篇原創文章 · 獲贊 7 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章