LoginSignup
1
2

More than 5 years have passed since last update.

【Unity拡張】Projectビューにnamespaceを表示するエディター拡張

Posted at

はじめに

スクリプトコードを書いてると、
「このスクリプトのnamespaceってなんだっけ?」
といったことがよくあります。

スクリプトをクリック選択してnamespaceを確認するのも悪くないですが、スクリプトの数が多いと確認するのも一苦労です。

そこで、Projectビューのスクリプトの横にnamespaceを表示するエディター拡張を作ってみました

つくったもの

スクリプトファイルの右側にスクリプトのnamespaceが表示されます。
image.png

環境

Unity2017.2.0f3
Windows 10

ソースコード

プロジェクト内にEditorフォルダを作成し以下のスクリプトを入れてください。

ProjectWindowNamespace.cs
namespace ProjectWindowNamespace
{
    using UnityEngine;
    using UnityEditor;

    public static class ProjectWindowNamespace
    {
        [InitializeOnLoadMethod]
        static void OnLoad()
        {
            EditorApplication.projectWindowItemOnGUI += ProjectWindowItemOnGUI;
        }

        static void ProjectWindowItemOnGUI(string guid, Rect rect)
        {
            var path = AssetDatabase.GUIDToAssetPath(guid);
            var script = AssetDatabase.LoadAssetAtPath(path, typeof(MonoScript)) as MonoScript;
            if (script == null) { return; }

            var scriptClass = script.GetClass();
            if (scriptClass == null) { return; }

            int width = 300;
            rect.x = rect.x + rect.width - width;
            rect.width = width;

            // namespaceを表示
            EditorGUI.LabelField(rect, scriptClass.Namespace);
        }
    }
}

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