うにてぃブログ

主にUnityとC#に関する記事を書いていきます

【Unity】ProjectWindow を拡張してみる

ProjectWindowにアイコンなどを表示する場合は以下のデリゲートを用いる
EditorApplication-projectWindowItemOnGUI - Unity スクリプトリファレンス

EditorWindowを継承したくないので、InitializeOnLoadMethodを利用する

スクリプト

サンプルとしてguidを最後に表示する拡張のスクリプトを貼っておく

using UnityEngine;
 
namespace UnityEditor
{
    public class ProjectWindowExtension
    {
        [InitializeOnLoadMethod]
        private static void ProjectWindow()
        {
            EditorApplication.projectWindowItemOnGUI += ProjectWindowGUI;
        }
  
        private static readonly GUIContent cacheContent = new GUIContent();
        private static Vector2 cacheSize;
        private static GUIStyle cacheStyle;
        private const int OFFSET = 5;
  
        private static void ProjectWindowGUI(string guid, Rect rect)
        {
            if (string.IsNullOrEmpty(guid))
                return;
            
            if (cacheStyle == null)
                cacheStyle = (GUIStyle) "AssetLabel";
                    
            cacheContent.text = guid;
            cacheSize = cacheStyle.CalcSize(cacheContent);
            cacheSize.x += OFFSET;
             
            rect.xMin = rect.xMax - cacheSize.x;
            rect.width = cacheSize.x - OFFSET;
            GUI.Label(rect, cacheContent, cacheStyle);
        }
    }
}

これを Unity に入れると以下のように表示される
f:id:hacchi_man:20200115132039p:plain