📗
【Unity】Hierarchyにアイコンを表示しよう
はじめに
初めての方も、そうでない方もこんにちは!
現役ゲームプログラマーのたむぼーです。
自己紹介を載せているので、気になる方は見ていただければ嬉しいです!
今回は
UnityのHierarchyにアイコンを表示する方法
を紹介します
実際の画面
こちらが、実際の画面です。
処理全体
任意のEditorフォルダに下記スクリプトを格納してください。
HierarchyIconViewSetting.cs
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEditor;
#if UNITY_EDITOR
public class HierarchyIconViewSetting
{
public const string MENU_PATH = "Tools/Enable HierarchyIconView";
[MenuItem(MENU_PATH)]
public static void ToggleEnable()
{
HierarchyIconView.enable = !HierarchyIconView.enable;
EditorPrefs.SetBool("HierarchyIconView.enable", HierarchyIconView.enable);
}
[MenuItem(MENU_PATH, true)]
public static bool ToggleEnableValidate()
{
Menu.SetChecked(MENU_PATH, HierarchyIconView.enable);
return true;
}
}
[InitializeOnLoad]
public class HierarchyIconView
{
public static bool enable = true;
private const int ICON_SIZE = 16;
private static System.Type[] visibleTypes = {
typeof(Collider),
typeof(Rigidbody),
typeof(Renderer),
typeof(Animator),
typeof(ParticleSystem),
typeof(Camera),
typeof(Canvas),
typeof(CanvasRenderer),
typeof(Light),
typeof(UIBehaviour),
typeof(Graphic),
};
static HierarchyIconView()
{
enable = EditorPrefs.GetBool("HierarchyIconView.enable", enable);
EditorApplication.hierarchyWindowItemOnGUI += ItemOnGUI;
}
public static void ItemOnGUI(int instanceID, Rect rect)
{
if (! enable)
{
return;
}
rect.x += rect.width;
rect.width = ICON_SIZE;
GameObject target = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
if (target == null)
{
return;
}
Component[] components = target.GetComponents<Component>();
HashSet<Texture> textures = new HashSet<Texture>();
foreach (Component component in components)
{
if (component == null)
{
continue;
}
foreach (System.Type visibleType in visibleTypes)
{
System.Type type = component.GetType();
if (type.IsSubclassOf(visibleType) || type == visibleType)
{
textures.Add(AssetPreview.GetMiniThumbnail(component));
}
}
}
foreach (Texture texture in textures)
{
if (texture == null)
{
continue;
}
rect.x -= ICON_SIZE;
GUI.DrawTexture(rect, texture);
}
}
}
#endif
Discussion