Lua IDE - IntelliJ IDEA+EmmyLua插件(2)

简介: Lua IDE - IntelliJ IDEA+EmmyLua插件

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


image.png

4.看到Unity工程项目会多出一个EmmyApi文件夹

5.将其打包成zip文件(注意不能是rar、7z其它压缩格式!)

image.png


6.在IDEA中点击File-Project Structure,Modules-选择我们的Modules-Dependencies,+号-Library-Lua Zip Library,选择我们刚才打包的zip文件。然后一直OK保存就行了。


image.png



7.测试Unity API提示功能:

image.png


成功!


其它功能


代码跳转:

image.png


智能重命名:

image.png


后续


本教程就到这里结束了,但是该插件还有许多有用的功能,可以自行探索,也可以加入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的作者们无私开源编写了这个强大的插件。


相关文章
|
24天前
|
人工智能 JavaScript 前端开发
字节最新AI 版IDE:用Trae开发网站打包信息追踪插件,国产版Cursor表现如何?
本文介绍了如何使用字节最新推出的AI编程工具Trae,通过零代码方式快速开发一款名为`dist-info`的前端插件。该插件能够将Git信息或自定义内容注入HTML文件中,兼容Webpack和Vite项目。开发者只需在浏览器控制台输入`info`,即可轻松查看代码的相关信息。文章详细描述了插件的背景、开发流程、核心代码实现以及优化建议,并展示了如何借助Trae高效完成项目搭建和代码编写。
166 0
|
3月前
|
IDE iOS开发 Python
小白如何开始使用通义灵码(含安装IDE、安装灵码插件)
PyCharm 和 IntelliJ IDEA 下载安装及通义灵码插件下载安装说明
3630 4
|
3月前
|
IDE 开发工具
【开发IDE升级】如何对IDEA版本进行升级
本文介绍了如何将 IntelliJ IDEA Ultimate 从 2020.2.2 版本升级到 2022.3.2 版本。主要内容包括准备工作、卸载旧版本和安装新版本的步骤。首先,从官网下载所需版本并备份旧版配置;接着,通过 Uninstall.exe 卸载旧版,保留配置和插件;最后,安装新版并完成激活。详细的操作步骤和截图帮助用户顺利完成升级过程。
4179 1
【开发IDE升级】如何对IDEA版本进行升级
|
5月前
|
数据可视化 开发者 索引
详解Wireshark LUA插件函数:function p_myproto.dissector(buffer, pinfo, tree)
在 Wireshark 中,LUA 插件通过 `function p_myproto.dissector(buffer, pinfo, tree)` 扩展协议解析能力,解析自定义应用层协议。参数 `buffer` 是 `PacketBuffer` 类型,表示原始数据包内容;`pinfo` 是 `ProtoInfo` 类型,包含数据包元信息(如 IP 地址、协议类型等);`tree` 是
215 1
|
7月前
|
IDE API 开发工具
通过IDE插件体验阿里云OpenAPI的高效集成, 精品礼品等你来拿!
轻量级的开放API工具——Alibaba Cloud Developer Toolkit及Alibaba Cloud API Toolkit。这些插件支持快速查阅阿里云产品的开放API,提供API调试与SDK示例生成等功能,帮助开发者轻松集成阿里云服务。您可通过JetBrains Marketplace或VS Code Marketplace搜索安装,完成身份验证后即刻体验。欢迎分享您的使用反馈,有机会获得精美礼品!
|
8月前
|
缓存 Java Maven
IntelliJ IDEA中无法加载jar包导致出现“cannot resolve symbol...”问题的解决
IntelliJ IDEA中无法加载jar包导致出现“cannot resolve symbol...”问题的解决
327 0
|
8月前
|
存储 Oracle Java
Java面试题:描述如何使用Eclipse或IntelliJ IDEA进行Java开发?
Java面试题:描述如何使用Eclipse或IntelliJ IDEA进行Java开发?
81 0
|
8月前
|
IDE Oracle Java
day4:JDK、IntelliJ IDEA的安装和环境变量配置
【7月更文挑战第4天】🏆本文收录于「滚雪球学Java」专栏,专业攻坚指数级提升,希望能够助你一臂之力,帮你早日登顶实现财富自由🚀;同时,欢迎大家关注&&收藏&&订阅!持续更新中,up!up!up!!
287 0
|
8月前
|
网络协议 安全 Linux
在IntelliJ IDEA中使用固定公网地址远程SSH连接服务器环境进行开发
在IntelliJ IDEA中使用固定公网地址远程SSH连接服务器环境进行开发
190 2
|
9月前
|
监控 IDE Java
探索 IntelliJ IDEA 中 Spring Boot 运行配置选项及其作用
探索 IntelliJ IDEA 中 Spring Boot 运行配置选项及其作用
876 0