修改Unity中Lua文件的默認打開程序

項目中引用了XLua,而Lua文件又是以txt文件結尾的,當修改系統的擴展腳本編輯器爲vs後雙擊lua文件(xx.txt)默認也使用vs打開了,無提示的黑白文本編輯

昨辦?

….

後來看到網上有寫Unity的插件,想着應該也能判斷後綴名然後調用指定的編輯器,果然可以。直接貼代碼了(C#文件,只要建一個名爲Editor的目錄 —— 與路徑無關,扔進去就行,Unity會自動編譯的)

using UnityEngine;
using UnityEditor;
using System;

public class LuaTxtEditor
{

    //http://www.xuanyusong.com/archives/3702 

    [UnityEditor.Callbacks.OnOpenAssetAttribute(1)]
    public static bool step1(int instanceID, int line)
    {
        //string name = EditorUtility.InstanceIDToObject(instanceID).name;
        //Debug.Log("Open Asset step: 1 (" + name + ")");

        return false;
    }

    [UnityEditor.Callbacks.OnOpenAssetAttribute(2)]
    public static bool step2(int instanceID, int line)
    {
        string strFilePath = AssetDatabase.GetAssetPath(EditorUtility.InstanceIDToObject(instanceID));
        string strFileName = Application.dataPath + "/" + strFilePath.Replace("Assets/", "");

        if (strFileName.EndsWith(".txt"))
        {
            string strZBStudioPath = Environment.GetEnvironmentVariable("ZEROBRANESTUDIO_PATH");

            if (strZBStudioPath != null && strZBStudioPath.Length > 0)
            {
                System.Diagnostics.Process process = new System.Diagnostics.Process();
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                startInfo.FileName = strZBStudioPath + (strZBStudioPath.EndsWith("/") ? "" : "/") +  "zbstudio.exe";
                startInfo.Arguments = strFileName;
                process.StartInfo = startInfo;
                process.Start();

                //Debug.Log(startInfo.FileName + " \t " + startInfo.Arguments);

                return true;
            }
            else
            {
                Debug.Log("Not Found Enviroment Variable 'ZEROBRANESTUDIO_PATH'.");

                return false;
            }            
        }

        //string name = EditorUtility.InstanceIDToObject(instanceID).name;
        //Debug.Log("Open Asset step: 1 (" + name + ")");

        return false;
    }

}

上面使用ZeroBraneStudio來打開lua文件,你也可以修改爲自己常用的編輯器,上面使用了環境變量獲取程序的安裝路徑。

另外介紹幾個小技巧:

1、shift + space(空格鍵),打以讓鼠標所停留的視窗最大化

2、Unity在運行模式(Play)下所做的修改是不保存的,爲了防止這種誤操作,可以修改運行模式下的顏色;

菜單Edit –> Preferences –> Colors –> playmode tint。

更多的技巧,可以參考知乎:Unity遊戲開發有哪些讓你拍案叫絕的技巧?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章