試了幾個Lua IDE後,Lua Studio、Lua Glider、VS+babelua插件、Sublime都不是特別滿意。直到發現了國人自創的另外一個神奇工具:基於IDEA的EmmyLua插件。該插件功能很是完整:斷點調試、自動提示、代碼跳轉、智能重命名等,能夠極大地提升Lua編程的速度。界面也比較舒服。git
安裝步驟
- 下載、安裝並破解IntelliJ IDEA(個人版本是目前最新的2017.3.4)
- 安裝EmmyLua插件
建議直接在IDEA工具內搜索插件安裝(固然也能夠下載插件到硬盤安裝)。
(1)在啓動界面點擊Configure-Plugins:
(2)搜索EmmyLua,而後點Search in repositories:
(3)點擊右邊的Install綠色按鈕。安裝完須要重啓IDEA。 - 配置SDK,默認是Path路徑須要有一個Lua解釋器(lua.exe)。你能夠在建立項目時指定其餘目錄。
- 配置其餘事項。
(1)將*.txt識別成lua文件:
(2)忽略代碼提示大小寫差異:
建立項目
-
建立普通Lua項目(不依賴其餘程序,如遊戲引擎):
New-Project,而後next,填項目名、路徑,點擊finish。
在項目視圖的src文件夾New一個Lua文件,能夠本身print()一下,Run一下,看看有沒有輸出,有的話,說明SDK配置正確。github
-
建立Unity引擎Lua項目
New-Modules from existing sources(注意不要選錯,這裏建立的是Modules,不是Project,不然等下導入不了api自動提示的library)。【這裏有一個IDE Bug:第一次建立Modules,會在文件夾裏生成一個.iml文件。可是若是文件夾裏原本就有.iml文件,之後再點Modules from existing sources就會沒法生成Modules,也就沒法導入library。這個Bug我折騰了一夜才發現的!必須刪掉.iml文件,才能夠從新建立Modules】
而後選擇Unity文件夾的Lua訪問根目錄,我選的是Resources文件夾,由於能夠從Resources做爲根目錄搜索lua文件。
而後咱們測試一下斷點調試功能。打開其中一個Lua文件,設置斷點:
而後Run-Attach To Local Process:
選擇Unity進程,觸發斷點,說明能斷點調試:編程
Unity API代碼提示
如今Unity API代碼提示是沒有的,由於咱們還沒導入API描述的library。這個library根據你選擇的Lua中間件不一樣而不一樣,因此建議是本身導出。個人Lua中間件是SLua。這裏以SLua爲例。
1.打開SLua官方自帶的Unity項目,在Slua-Editor下面,新建一個SLuaApiExporter.cs腳本:
2.輸入以下代碼:api
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; } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
3.在Unity編輯器中點擊SLua-導出EmmyLuaApi babel
4.看到Unity工程項目會多出一個EmmyApi文件夾: 編輯器
5.將其打包成zip文件(注意不能是rar、7z其它壓縮格式!) ide
6.在IDEA中點擊File-Project Structure,Modules-選擇咱們的Modules-Dependencies,+號-Library-Lua Zip Library,選擇咱們剛纔打包的zip文件。而後一直OK保存就好了。 工具
7.測試Unity API提示功能: 測試
成功!ui
其它功能
代碼跳轉:
智能重命名:
後續
本教程就到這裏結束了,可是該插件還有許多有用的功能,能夠自行探索,也能夠加入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的做者們無私開源編寫了這個強大的插件。
參考連接:https://blog.csdn.net/sinat_24229853/article/details/79226608
https://blog.csdn.net/u010019717/article/details/77510066?ref=myread