unity3d 右鍵快速打開文件

在開發中如果只是想快速查看某個(如.lua)文件的話,可以活用右鍵功能,這個在打開多個工程並調試的情況下略顯高效。

如圖:

寫了一個工具類,並添加了兩個方法:可選用notepad++或記事本快速打開文件。

代碼如下:

using UnityEngine;
using System.Collections;
using UnityEditor;
using thisObject = UnityEngine.Object;
using System.Threading;
using System;

public class EasyTool
{
    const int OpenMax = 10; //一次打開文件的最大數量

    const string NotePadJJ_APP_NAME = "notepad++.exe";
    const string NotePad_APP_NAME = "notepad.exe";

    /// <summary>
    /// 用notepad++打開文件
    /// </summary>
    [MenuItem("Assets/EasyTool/Open_NotePad++")]
    static public void OpenForNotePadJJ()
    {
        int count = 0;
        foreach (var go in GetSelectObject())
        {
            if (go != null)
            {
                string dir_path = GetPath(go);

                InvokeCmd(NotePadJJ_APP_NAME, dir_path);
            }

            count++;

            if (count > OpenMax)
            {
                break;
            }
        }
    }

    // <summary>
    /// 用記事本打開文件
    /// </summary>
    [MenuItem("Assets/EasyTool/Open_NotePad")]
    static public void OpenForNotePad()
    {
        int count = 0;
        foreach (var go in GetSelectObject())
        {
            if (go != null)
            {
                string dir_path = GetPath(go);

                InvokeCmd(NotePad_APP_NAME, dir_path);

                count++;

                if (count > OpenMax)
                {
                    break;
                }
            }
        }
    }


    /// <summary>
    /// 調用CMD 命令
    /// </summary>
    public static void InvokeCmd(string cmd, string dir_path)
    {
        UnityEngine.Debug.Log(cmd);
        AssetDatabase.Refresh();
        new Thread(new ThreadStart(() =>
        {
            try
            {
                System.Diagnostics.Process p = new System.Diagnostics.Process();
                p.StartInfo.FileName = cmd;
                p.StartInfo.Arguments = dir_path;
                p.Start();
                p.WaitForExit();
                p.Close();
            }
            catch (Exception e)
            {
                Debug.Log(e.Message);
            }
        })).Start();
    }


    /// <summary>
    /// 獲取選擇的文件
    /// </summary>
    /// <returns></returns>
    static public thisObject[] GetSelectObject()
    {
        if (Selection.objects.Length == 0)
        {
            return new thisObject[0];
        }

        return Selection.objects;
    }


    /// <summary>
    /// 獲取文件路徑
    /// </summary>
    /// <param name="go"></param>
    /// <returns></returns>
    static public string GetPath(thisObject go)
    {
        string str = Application.dataPath.Replace("Assets", "");
        string path = AssetDatabase.GetAssetPath(go);
        string dir_path = System.IO.Path.GetFullPath(str + path);
        return dir_path;
    }

}


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