zoukankan      html  css  js  c++  java
  • 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打开

  • 相关阅读:
    c++ readIntger writeIntger
    c++ string split function
    uniapp 创建简单的tabs
    c++ 获取和设置 窗口标题
    两种js数组去重的方法
    将jquery和公共样式缓存到localStorage,可以减少Http请求,从而优化页面加载时间
    H5 多个视频 循环播放效果
    使用iScroll时,input等不能输入内容的解决方法
    修改EsayUi 中 tree 的原有样式,变为according 之类的样式 ,且子菜单显示在右侧
    跨浏览器的事件对象-------EventUtil 中的方法及用法
  • 原文地址:https://www.cnblogs.com/StraussDu/p/7453798.html
Copyright © 2011-2022 走看看