Unity API代码提示
现在Unity API代码提示是没有的,因为我们还没导入API描述的library。这个library根据你选择的Lua中间件不同而不同,所以建议是自己导出。我的Lua中间件是SLua。这里以SLua为例。
1.打开SLua官方自带的Unity项目,在Slua-Editor下面,新建一个SLuaApiExporter.cs脚本:
2.输入如下代码:
using System; using System.Reflection; using System.Collections.Generic; using System.Linq; using System.Text; using UnityEditor; using SLua; using System.IO; using UnityEngine; namespace Slua { public static class EmmyLuaApiExporter { [MenuItem("SLua/导出EmmyLuaApi", false, 14)] static void Gen() { string path = "./EmmyApi/"; if (Directory.Exists(path)) { Directory.Delete(path, true); } Directory.CreateDirectory(path); //UnityEngine GenAssembly("UnityEngine", path); //GenAssembly("UnityEngine.UI", path); GenCustom(path); } public static void GenAssembly(string name, string path) { List<string> excludeList; List<string> includeList; CustomExport.OnGetNoUseList(out excludeList); CustomExport.OnGetUseList(out includeList); Type[] types = Assembly.Load(name).GetTypes(); foreach (Type t in types) { if (LuaCodeGen.filterType(t, excludeList, includeList)) { GenType(t, false, path); } } } public static void GenCustom(string path) { Type[] types = Assembly.Load("Assembly-CSharp-firstpass").GetTypes(); foreach (Type t in types) { if (t.IsDefined(typeof(CustomLuaClassAttribute), false)) { GenType(t, true, path); } } types = Assembly.Load("Assembly-CSharp").GetTypes(); foreach (Type t in types) { if (t.IsDefined(typeof(CustomLuaClassAttribute), false)) { GenType(t, true, path); } } } public static void GenType(Type t, bool custom, string path) { if (!CheckType(t, custom)) return; //TODO System.MulticastDelegate var sb = new StringBuilder(); if (!CheckType(t.BaseType, custom)) sb.AppendFormat("---@class {0}\n", t.Name); else sb.AppendFormat("---@class {0} : {1}\n", t.Name, t.BaseType.Name); GenTypeField(t, sb); sb.AppendFormat("local {0}={{ }}\n", t.Name); GenTypeMehod(t, sb); sb.AppendFormat("{0}.{1} = {2}", t.Namespace, t.Name, t.Name); File.WriteAllText(path + t.FullName + ".lua", sb.ToString(), Encoding.UTF8); } static bool CheckType(Type t, bool custom) { if (t == null) return false; if (t == typeof(System.Object)) return false; if (t.IsGenericTypeDefinition) return false; if (t.IsDefined(typeof(ObsoleteAttribute), false)) return false; if (t == typeof(YieldInstruction)) return false; if (t == typeof(Coroutine)) return false; if (t.IsNested) return false; if (custom && !t.IsDefined(typeof(CustomLuaClassAttribute), false)) return false; return true; } public static void GenTypeField(Type t, StringBuilder sb) { FieldInfo[] fields = t.GetFields(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach(var field in fields) { if (field.IsDefined(typeof(DoNotToLuaAttribute), false)) continue; sb.AppendFormat("---@field public {0} {1}\n", field.Name, GetLuaType(field.FieldType)); } PropertyInfo[] properties = t.GetProperties(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (var pro in properties) { if (pro.IsDefined(typeof(DoNotToLuaAttribute), false)) continue; sb.AppendFormat("---@field public {0} {1}\n", pro.Name, GetLuaType(pro.PropertyType)); } } public static void GenTypeMehod(Type t, StringBuilder sb) { MethodInfo[] methods = t.GetMethods(BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.DeclaredOnly); foreach (var method in methods) { if (method.IsGenericMethod) continue; if (method.IsDefined(typeof(DoNotToLuaAttribute), false)) continue; if(method.Name.StartsWith("get_") || method.Name.StartsWith("set_")) continue; sb.AppendLine("---@public"); var paramstr = new StringBuilder(); foreach (var param in method.GetParameters()) { sb.AppendFormat("---@param {0} {1}\n", param.Name, GetLuaType(param.ParameterType)); if (paramstr.Length != 0) { paramstr.Append(", "); } paramstr.Append(param.Name); } sb.AppendFormat("---@return {0}\n", method.ReturnType == null ? "void" : GetLuaType(method.ReturnType)); if( method.IsStatic) { sb.AppendFormat("function {0}.{1}({2}) end\n", t.Name, method.Name, paramstr); } else { sb.AppendFormat("function {0}:{1}({2}) end\n", t.Name, method.Name, paramstr); } } } static string GetLuaType(Type t) { if (t.IsEnum //|| t == typeof(ulong) //|| t == typeof(long) //|| t == typeof(int) //|| t == typeof(uint) //|| t == typeof(float) || t == typeof(double) //|| t == typeof(byte) //|| t == typeof(ushort) //|| t == typeof(short) ) return "number"; if (t == typeof(bool)) return "bool"; if (t == typeof(string)) return "string"; if (t == typeof(void)) return "void"; return t.Name; } } }
3.在Unity编辑器中点击SLua-导出EmmyLuaApi
4.看到Unity工程项目会多出一个EmmyApi文件夹
5.将其打包成zip文件(注意不能是rar、7z其它压缩格式!)
6.在IDEA中点击File-Project Structure,Modules-选择我们的Modules-Dependencies,+号-Library-Lua Zip Library,选择我们刚才打包的zip文件。然后一直OK保存就行了。
7.测试Unity API提示功能:
成功!
其它功能
代码跳转:
智能重命名:
后续
本教程就到这里结束了,但是该插件还有许多有用的功能,可以自行探索,也可以加入EmmyLua的官方QQ群:29850775。群里面有许多教程,本文所用的API导出代码也是从群文件里拿出来改的。
github: https://github.com/tangzx/IntelliJ-EmmyLua
oschina: http://git.oschina.net/tangzx/IntelliJ-Lua
IDEA Plugins : https://plugins.jetbrains.com/plugin/9768-emmylua
最后感谢EmmyLua的作者们无私开源编写了这个强大的插件。