2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Unity】キー入力1つでGameObjectにアタッチされているすべてのスクリプトを開く

Last updated at Posted at 2016-06-28

※この記事のUnityのバージョンは5.3.5f1です

はじめに

ゲームオブジェクトにアタッチされたスクリプトを開くとき、マウスを使って
ダブルクリックするの面倒くさいですよね。
そこで、選択しているオブジェクトにアタッチされている全てのスクリプトを
1回のキー入力で開けるようにするエディター拡張を作ってみました。

ソースコード

以下のスクリプトをUnityプロジェクトに入れるだけで使えるようになります.

EditorOpenComponents.cs
# if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
using System.Reflection;

[InitializeOnLoad]
public class EditorOpenComponents
{
  static EditorOpenComponents()
  {
    bool keyDown = false;
    EditorApplication.CallbackFunction function = () =>
    {
      if (!keyDown && Event.current.type == EventType.KeyDown)
      {
        keyDown = true;

        // . が入力されたらHierarchyで選択しているオブジェクトにアタッチされているすべてのスクリプトを開く
        if (Event.current.keyCode == KeyCode.Period && Selection.activeGameObject != null)
        {
          var mss = Resources.FindObjectsOfTypeAll<MonoScript>();

          foreach (var ms in mss)
          {
            var cls = ms.GetClass();

            if (cls != null)
            {
              if (cls.IsSubclassOf(typeof(MonoBehaviour)) && Selection.activeGameObject.GetComponent(cls) != null)
              {
                // スクリプトを開く
                OpenInEditor(cls.ToString(), 0);
              }
            }
          }
        }
      }

      if (keyDown && Event.current.type == EventType.KeyUp)
      {
        keyDown = false;
      }
    };

    FieldInfo info = typeof(EditorApplication).GetField("globalEventHandler", BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);
    EditorApplication.CallbackFunction functions = (EditorApplication.CallbackFunction)info.GetValue(null);
    functions += function;
    info.SetValue(null, (object)functions);
  }

  /// <summary>
  /// スクリプトを外部エディタで開く
  /// </summary>
  public static void OpenInEditor(string scriptName, int scriptLine)
  {
    string[] paths = AssetDatabase.GetAllAssetPaths();

    foreach (string path in paths)
    {
      string scriptPath = System.IO.Path.GetFileNameWithoutExtension(path);
      if (scriptPath.Equals(scriptName))
      {
        MonoScript script = AssetDatabase.LoadAssetAtPath(path, typeof(MonoScript)) as MonoScript;
        if (script != null)
        {
          if (!AssetDatabase.OpenAsset(script, scriptLine))
          {
            Debug.LogWarning("Couldn't open script : " + scriptName);
          }
          break;
        }
        else
        {
          Debug.LogWarning("Couldn't open script : " + scriptName);
        }
        break;
      }
    }
  }
}
# endif // UNITY_EDITOR

使い方

  1. HierarchyのGameObjectを選択。

  1. キーボードの . を押す。

  1. 押すと選択しているGameObjectにアタッチされているすべてのスクリプトが外部エディタで開かれます。
筆者はVisualStudioをエディタに設定しているのでVisualStudioが起動しました.
便利かも?

参考

UnityEditorのグローバルEventを取得する - けいごのなんとか
http://anchan828.hatenablog.jp/entry/2013/05/20/135339

2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?