目次
はじめに
Unity の Hierarchy 上で、オブジェクトに付随するコンポーネント一覧をアイコン表示するエディター拡張の紹介です。
Unity 環境
Unity 2020.2.0f1
コンポーネント一覧をアイコン表示した際の見た目
本対応により、オブジェクトのコンポーネント一覧がアイコン形式で表示されるようになります。

サンプルコード紹介
using UnityEngine;
using UnityEditor;
public static class HierarchyGUI_ShowComponent
{
private const int ICON_SIZE = 16;
[InitializeOnLoadMethod]
private static void Initialize()
{
EditorApplication.hierarchyWindowItemOnGUI += OnGUI;
}
private static void OnGUI(int instanceID, Rect selectionRect)
{
// instanceID をオブジェクト参照に変換
var go = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (go == null)
{
return;
}
// オブジェクトが所持しているコンポーネント一覧を取得
var components = go.GetComponents<Component>();
if (components.Length == 0)
{
return;
}
selectionRect.x = selectionRect.xMax - ICON_SIZE * components.Length;
selectionRect.width = ICON_SIZE;
foreach (var component in components)
{
// コンポーネントのアイコン画像を取得
var texture2D = AssetPreview.GetMiniThumbnail(component);
GUI.DrawTexture(selectionRect, texture2D);
selectionRect.x += ICON_SIZE;
}
}
}
補足情報
AssetPreview.GetMiniThumbnail
でアイコン画像を取得しています。
var texture2D = AssetPreview.GetMiniThumbnail(component);
このアイコンは Project ビューや Inspector ビューで表示されているものと同じです。


あわせて読みたい