LoginSignup
7
2

More than 5 years have passed since last update.

[Unity Editor拡張]いつものアプリケーションで開くメニューを追加

Last updated at Posted at 2018-02-26

Unityでシェーダーを描いたりするとき、UnityのProjectウィンドウのアイテムをダブルクリックすることで
エディタで編集したりするわけですが、一部の拡張子だけ融通がきかない!

.png → プレビューが開く(分かってんじゃん)
.cs → Riderが開く(まぁ、Unityのエディタ設定そうなってるしね)
.shader → Riderが開く(ん〜...個人的にSublime Textが開いてほしい...)

↑ここの所が結構カユイところに手がとどかないんですよね.
ということでそれらを解消するためにいつものアプリ(Defaut Application)で開くメニューを作ります。

[1] Finderでデフォルトアプリケーションを設定

まず、システムのデフォルトアプリケーション設定をします。デフォルトアプリケーションというのは、それぞれの拡張子に紐付けられるアプリケーションのことですね。.htmlならchromeみたいな.

手元にあったのがmac環境なのでそちらで説明します。
まずFinderで.shaderなどのUnityで使う拡張子のファイルを選択し
スクリーンショット 2018-02-26 12.53.05.png

情報を見るを選択
スクリーンショット 2018-02-26 12.53.28.png

詳細情報が出るので、そこの中にあるこのアプリケーションで開くという項目から
デフォルトアプリを設定します。私の場合shaderをsublime textで編集したいのでsublime textを選択しました。
その後特に問題がなければすべてを変更...を押します。
スクリーンショット 2018-02-26 12.53.57.png

このように設定することでデフォルトアプリケーションが設定できました。

[2] Unityを開きAssets/Editorにcsファイルを置く

エディタ拡張用のファイルを起きます。Assets/EditorはUnity上では特殊フォルダという扱いでエディタ拡張用のスクリプトは普通はそこに置きます。

では以下のC#スクリプトをコピペして下さい。
https://gist.github.com/nmxi/838ddea06cf90039b53c8b148788453c

EDITOR_FileOpenInApp.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using System.Diagnostics;

//システムのデフォルトアプリケーションでファイルを開く機能を
//Projectウィンドウのファイル選択→右クリック、または左上のAssetsボタンから呼ぶことができる
public class EDITOR_FileOpenInApp: EditorWindow {

    [MenuItem("Assets/Open in System Default Application")]
    private static void GetSelectFile(){

        if(Selection.assetGUIDs != null && Selection.assetGUIDs.Length > 0){
            List<string> fileList = new List<string>();

            foreach(var files in Selection.assetGUIDs){
                var path = AssetDatabase.GUIDToAssetPath(files);
                fileList.Add(path);
            }

            foreach (string directory in fileList) {
                Command("open " + Application.dataPath + directory.Substring(6, directory.Length - 6));
            }
        }
    }

    static string Command(string cmd){
        var p = new Process();
        p.StartInfo.FileName = "/bin/bash";
        p.StartInfo.Arguments = "-c \" " + cmd + " \"";
        p.StartInfo.UseShellExecute = false;
        p.StartInfo.RedirectStandardOutput = true;
        p.Start();

        var output = p.StandardOutput.ReadToEnd();
        p.WaitForExit();
        p.Close();

        return output;
    }
}

こんな感じに置けましたでしょうか.
スクリーンショット 2018-02-26 13.07.02.png

正常に置かれていれば、Projectウィンドウでファイル選択→右クリックをすることで以下のような
Open in System Default Applicationというボタンが表示されます。
スクリーンショット 2018-02-26 13.10.23.png

これを押すことでUnity上のエディタ設定とは例外のアプリケーションでファイルを開くことが可能になります。

7
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
2