LoginSignup
1
0

More than 3 years have passed since last update.

【Unity:エディタ拡張】EditorWindowからスクリプトを開く

Last updated at Posted at 2018-01-19

概要

EditorWindow作成して
スクリプト編集しようって時にいちいちスクリプトファイル探して開くの面倒なので対応した

※変更:パス直書きだとイケてないので取得するように対応してみた

サンプル

スクリプト
using System.Runtime.CompilerServices;
using UnityEditor;
using UnityEngine;

public class EditorSample : EditorWindow, IHasCustomMenu
{
    public void AddItemsToMenu(GenericMenu menu)
    {
        // メニュー追加
        menu.AddItem(new GUIContent("Open Script"), false, () =>
        {
            //UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath("Assets/Scripts/Editor/Sample.cs", typeof(UnityEngine.Object));
            string filePath = GetSourceFilePath();
            filePath = filePath.Replace(Application.dataPath.Substring(0, Application.dataPath.LastIndexOf("/") + 1), "");
            UnityEngine.Object obj = AssetDatabase.LoadAssetAtPath(filePath, typeof(UnityEngine.Object));
            if (obj != null)
            {
                Selection.activeObject = obj;
                // 第2引数は行数
                AssetDatabase.OpenAsset(obj, 0);
            }
        });
    }

    private string GetSourceFilePath([CallerFilePath] string sourceFilePath = "")
    {
        return sourceFilePath.Replace("\\", "/");
    }
}

additem.png
ウィンドウ右上のボタンかタブ右クリックで開くメニューに追加される

1
0
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
1
0