使用LuaFramework

修改为即时生效

Assets\LuaFramework\Scripts\ConstDefine\AppConst.cs中的LuaBundleMode修改为false,代码文件便不会以AssetBundle模式读取,会直接生效,以方便调试。


修改lua路径

直接使用IDEA读取LuaFramework的Lua目录会多出很多meta后缀的文件,所以把Lua目录移动至Assets文件夹外面

使用LuaFramework_第1张图片

然后使用del /a /f /s /q *.meta命令删除多余文件

打开LuaFramework/ToLua/Source/LuaConst.cs,把 luaDir 路径修改为

public static string luaDir = Application.dataPath.Replace("Assets","")+"Lua";

打开LuaFramework/Editor/CustomSettings.cs,把 luaDir 路径修改为

    public static string luaDir = LuaConst.luaDir;//FrameworkPath + "/Lua/";

打开LuaFramework/Editor/Packager.cs,把HandleLuaFile函数的luaPaths修改为

        string[] luaPaths = { CustomSettings.luaDir,
                              AppDataPath + "/LuaFramework/Tolua/Lua/" };

 

然后再使用IDEA读取Lua目录,File》New》Modules from existing sources,找到Lua文件夹创建就行

如果Lua本来就带有.iml文件需要先删除


IDEA添加智能补全

使用代码生成Lua文件后,找到项目目录下的LuaLib文件夹,把Lua文件打成ZIP包导入IDEA即可

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

public class GenLuaApi : EditorWindow
{
    private string namespaceName = "UnityEngine";
    private string appPath;

    StringBuilder sb = new StringBuilder();


    [MenuItem("Lua/GenLuaAPI")]
    static void OnInit()
    {
        var window = GetWindow();
        window.Show();
    }

    private void OnEnable()
    {
        appPath = Application.dataPath.Replace("Assets", "");
    }

    private void OnGUI()
    {
        EditorGUILayout.Space();
        namespaceName = EditorGUILayout.TextField("请输入命名空间:", namespaceName, GUILayout.MinHeight(50));
        EditorGUILayout.Space();
        if (GUILayout.Button("GenLua", GUILayout.MinHeight(50)))
        {
            var types = Assembly.Load(namespaceName + ".CoreModule").GetTypes();
            var dir = appPath + "LuaLib/" + namespaceName + "/";
            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }

            foreach (var type in types)
            {
                if (!type.IsPublic) continue;
                if (type.IsEnum ||
                    type.FullName.IndexOf("<") != -1) continue;
                if (type.BaseType != null)
                {
                    if (type.BaseType == typeof(System.MulticastDelegate))
                        continue;
                }
                ForeachType(type);
                var file = dir + GetString(type.ToString()) + ".lua";
                if (!File.Exists(file)) File.Delete(file);
                File.WriteAllText(file, sb.ToString(), Encoding.UTF8);
                sb.Length = 0;
            }
            Debug.Log(">>>>>>>>>OK");
        }
    }

    void ForeachType(Type type)
    {
        sb.AppendFormat("---@class {0} : {1}\n", GetString(type.ToString()), type.BaseType);

        ForeachFields(type);

        sb.Append("local m={}\n\n");

        var ms = type.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public);
        Dictionary> dic = new Dictionary>();
        foreach (var m in ms)
        {
            if (!dic.ContainsKey(m.Name))
            {
                dic.Add(m.Name, new List());
            }
            dic[m.Name].Add(m);
        }
        foreach (var list in dic)
        {
            if (list.Key.IndexOf("get_") != -1 || list.Key.IndexOf("set_") != -1) continue;
            var methodInfo = list.Value[0];

            if (list.Value.Count > 1)
            {
                for (int i = 1, count = list.Value.Count; i < count; i++)
                {
                    sb.AppendFormat("---@overload fun({0})\n", GetParameterString(list.Value[i]));
                }
            }
            if (methodInfo.IsStatic)
            {
                sb.Append("---@static\n");
            }
            StringBuilder sp = new StringBuilder();
            foreach (var par in methodInfo.GetParameters())
            {
                sp.AppendFormat("{0},", par.Name);
                sb.AppendFormat("---@param {0} {1}\n", par.Name, GetTypeName(par.ParameterType));
            }
            if (sp.Length > 0) sp.Length--;
            if (methodInfo.ReturnType != typeof(void))
                sb.AppendFormat("---@return {0}\n", GetTypeName(methodInfo.ReturnType));
            sb.AppendFormat("function m:{0}({1})end\n\n", methodInfo.Name, sp.ToString());
            sp.Length = 0;
        }

        sb.AppendFormat("{0} = m\n", GetString(type.ToString()));
        sb.Append("return m");
    }

    void ForeachFields(Type type)
    {
        var fields = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public);
        foreach (var field in fields)
        {
            if (field.Name.IndexOf("<") != -1) continue;
            sb.AppendFormat("---@field public {0} {1}\n", field.Name, GetTypeName(field.FieldType));
        }
        var p = type.GetProperties(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public);
        foreach (var field in p)
        {
            sb.AppendFormat("---@field public {0} {1}\n", field.Name, GetTypeName(field.PropertyType));
        }
    }

    string GetTypeName(Type type)
    {
        var name = type.ToString();
        switch (name)
        {
            case "System.Int32":
            case "System.Byte":
            case "System.Double":
            case "System.Single":
            case "System.Int64":
            case "System.UInt32":
            case "System.UInt64":
            case "System.Int16":
            case "System.UInt16":
            case "System.Decimal": return "number";
            case "System.Boolean": return "boolean";
            case "System.String": return "string";
        }

        if (name.IndexOf("System.Collections.Generic.List") != -1)
        {
            return GetTypeName(type.GetProperties()[2].PropertyType) + "[]";
        }
        if (name.IndexOf("System.Action") != -1)
        {
            name = Regex.Match(name, @"\[(.*)\]").Value.Replace("[", "").Replace("]", "");
        }
        if (name.IndexOf("System.Collections.Generic.IEnumerable") != -1)
        {
            return "System.Collections.Generic.IEnumerable";
        }
        if (name.IndexOf("+") != -1)
        {
            if (type.BaseType == typeof(System.MulticastDelegate))
            {
                return "fun()";
            }
            else
            {
                return name.Replace("+", ".");
            }
        }
        name = name.Replace("&", "");
        return name;
    }


    string GetParameterString(MethodInfo type)
    {
        StringBuilder s = new StringBuilder();
        foreach (var par in type.GetParameters())
        {
            s.AppendFormat("{0}:{1},", par.Name, GetTypeName(par.ParameterType));
        }
        if (s.Length > 0)
            s.Length--;
        return s.ToString();
    }


    string GetString(string value)
    {
        value = value.Replace("`", "_");
        value = value.Replace("[", "_");
        value = value.Replace("]", "_");
        value = value.Replace(",", "_");
        value = value.Replace("+", ".");
        return value;
    }
}

也可以使用这两个打包好的

UnityEngine命名空间的lua声明文件

momo命名空间的lua声明文件

 


修改资源打包

打开LuaFramework/Editor/Packager.cs,添加

static string appDir = Application.dataPath.Replace("Assets", "").Replace("/", "\\");
    static void AddBuildMaps(string dirPath,string pattern,string nameSuffix="" )
    {
        var dirInfo = new DirectoryInfo(Application.dataPath + "/" + dirPath);
        var dirs = dirInfo.GetDirectories();
        foreach (var dir in dirs)
        {
            AddBuildMap(dir.Name.ToLower()+ nameSuffix + AppConst.ExtName, pattern, dir.FullName.Replace(appDir, ""));
        }
    }

修改HandleExampleBundle为

    static void HandleExampleBundle() {
        string resPath = AppDataPath + "/" + AppConst.AssetDir + "/";
        if (!Directory.Exists(resPath)) Directory.CreateDirectory(resPath);

        AddBuildMaps("LuaFramework/Examples/Builds","*.prefab");
        AddBuildMaps("LuaFramework/Examples/Textures", "*.png", "_asset");
    }

 


移除栗子

打开LuaFramework/Scripts/ConstDefine/AppConst.cs 把例子模式设置为false

打开LuaFramework/Scripts/Manager/GameManager.cs,的OnInitialize函数改为

        void OnInitialize() {
            
            LuaManager.InitStart();
            LuaManager.DoFile("Logic/Game");         
            Util.CallMethod("Game", "OnInitOK");
            initialize = true;
        }

 


待续

 

 

 

 

 

 

 

你可能感兴趣的:(unity3d,LuaFramework,unity)