UnityProject面板中右键创建新的Lua脚本以及修改文件默认打开方式的编辑器

  新大厅中要介入热更新,那自然少不了Lua。这两天在学习腾讯的xLua还有之前一直有在积累的shader知识。由于Unity原生没有直接创建lua脚本的功能,而且VS对Lua和Shader支持的都不是特别好,而且比较臃肿,所以我制作了创建lua脚本和改变其默认打开方式的编辑器。

1.右键创建Lua脚本

 1     [MenuItem("Assets/Create/Lua Script #&c", priority = 81)]
 2     static void CreateLuaScriptMethod()
 3     {
 4         Object[] obj = Selection.objects;
 5         
 6         string assetPath = AssetDatabase.GetAssetPath(obj[0]);
 7         string path = "";
 8         if (!Path.HasExtension(assetPath))
 9         {
10             path = Path.GetFullPath(assetPath);
11         }
12         else
13         {
14             string dirPath = Path.GetDirectoryName(assetPath);
15             path = Path.GetFullPath(dirPath);
16         }
17         path += "/NewLuaScript.lua.txt";
18         int index = 1;
19         while (File.Exists(path))
20         {
21             string dir = Path.GetDirectoryName(path);
22             path = dir + "/NewLuaScript"+index+".lua.txt";
23             index++;
24         }
25         Debug.Log(path);
26         StreamWriter sw = new StreamWriter(path, false);
27         sw.Close();
28         AssetDatabase.Refresh();
29     }

  priority 是设置该条目的优先级,为了让它在创建C#脚本下方

  在文件上右键时在它的目录下生成新的lua.txt文件(txt是为了能被Unity识别为TextAsset,xLua的脚本就是这种格式)。当命名重复会自动在后面增加index。

2.将Shader文件的打开方式更换为系统默认的打开方式

[OnOpenAsset]
    public static bool OpenShaderAsset(int instanceID, int line)
    {
        if (EditorUtility.InstanceIDToObject(instanceID) is Shader)
        {
            string assetPath = AssetDatabase.GetAssetPath(instanceID);
            System.Diagnostics.Process.Start(Application.dataPath + "/../" + assetPath);
            return true;
        }
        return false;
    }

  系统默认打开方式人人应该都会了,想让.txt文件也用默认方式打开再创建一个函数,把Shader改成TextAsset即可。这样我的lua脚本和Shader脚本都会有VSCode打开

转载于:https://www.cnblogs.com/StraussDu/p/7453798.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值