【Unity】【エディタ拡張】Hierarchyの表示を拡張するためのUtility

自分用メモ。更新していくかもしれません。

Unity2018.3.1

ソースコード

using UnityEngine;
using UnityEditor;

public static class HierarchyGUIUtility
{
    public enum IconType
    {
        Warning,
        Error,
        Info,
        CircleGrey,
        CircleBlue,
        CircleCyan,
        CircleGreen,
        CircleYellow,
        CircleOrange,
        CircleRed,
        CircleMagenta,
        DiamondGrey,
        DiamondBlue,
        DiamondCyan,
        DiamondGreen,
        DiamondYellow,
        DiamondOrange,
        DiamondRed,
        DiamondMagenta,
        Settings,
        Test,
    }

    public static bool DrawIconButton(IconType iconType, ref Rect targetRect)
    {
        return DrawIconButton(GetIconTexture(iconType), ref targetRect);
    }

    public static bool DrawIconButton(Texture texture, ref Rect targetRect)
    {
        var drawRect = targetRect;
        drawRect.xMin += drawRect.width - EditorGUIUtility.singleLineHeight;
        drawRect.width = EditorGUIUtility.singleLineHeight;
        targetRect.xMax -= EditorGUIUtility.singleLineHeight;
        return GUI.Button(drawRect, texture, new GUIStyle());
    }

    private static Texture GetIconTexture(IconType textureType)
    {
        switch (textureType) {
        case IconType.Warning:
            return EditorGUIUtility.Load("console.warnicon") as Texture2D;
        case IconType.Error:
            return EditorGUIUtility.Load("console.erroricon") as Texture2D;
        case IconType.Info:
            return EditorGUIUtility.Load("console.infoicon") as Texture2D;
        case IconType.CircleGrey:
            return EditorGUIUtility.Load("sv_icon_dot0_pix16_gizmo") as Texture2D;
        case IconType.CircleBlue:
            return EditorGUIUtility.Load("sv_icon_dot1_pix16_gizmo") as Texture2D;
        case IconType.CircleCyan:
            return EditorGUIUtility.Load("sv_icon_dot2_pix16_gizmo") as Texture2D;
        case IconType.CircleGreen:
            return EditorGUIUtility.Load("sv_icon_dot3_pix16_gizmo") as Texture2D;
        case IconType.CircleYellow:
            return EditorGUIUtility.Load("sv_icon_dot4_pix16_gizmo") as Texture2D;
        case IconType.CircleOrange:
            return EditorGUIUtility.Load("sv_icon_dot5_pix16_gizmo") as Texture2D;
        case IconType.CircleRed:
            return EditorGUIUtility.Load("sv_icon_dot6_pix16_gizmo") as Texture2D;
        case IconType.CircleMagenta:
            return EditorGUIUtility.Load("sv_icon_dot7_pix16_gizmo") as Texture2D;
        case IconType.DiamondGrey:
            return EditorGUIUtility.Load("sv_icon_dot8_pix16_gizmo") as Texture2D;
        case IconType.DiamondBlue:
            return EditorGUIUtility.Load("sv_icon_dot9_pix16_gizmo") as Texture2D;
        case IconType.DiamondCyan:
            return EditorGUIUtility.Load("sv_icon_dot10_pix16_gizmo") as Texture2D;
        case IconType.DiamondGreen:
            return EditorGUIUtility.Load("sv_icon_dot11_pix16_gizmo") as Texture2D;
        case IconType.DiamondYellow:
            return EditorGUIUtility.Load("sv_icon_dot12_pix16_gizmo") as Texture2D;
        case IconType.DiamondOrange:
            return EditorGUIUtility.Load("sv_icon_dot13_pix16_gizmo") as Texture2D;
        case IconType.DiamondRed:
            return EditorGUIUtility.Load("sv_icon_dot14_pix16_gizmo") as Texture2D;
        case IconType.DiamondMagenta:
            return EditorGUIUtility.Load("sv_icon_dot15_pix16_gizmo") as Texture2D;
        case IconType.Settings:
            return EditorGUIUtility.Load("EditorSettings Icon") as Texture2D;
        default:
            throw new System.Exception("invalid " + textureType.ToString());
        }
    }
}

使い方

EditorApplication.hierarchyWindowItemOnGUIにコールバック登録してこんな感じに使います。

using UnityEngine;
using UnityEditor;

public class HierarchyGUI  
{
    [InitializeOnLoadMethod]
    private static void AddHierarchyItemOnGUI() {
        EditorApplication.hierarchyWindowItemOnGUI += HierarchyWindowItemOnGUI;
    }

    private static void HierarchyWindowItemOnGUI (int instanceID, Rect selectionRect) 
    {
        var guiRect = selectionRect;
        HierarchyGUIUtility.DrawIconButton(HierarchyGUIUtility.IconType.Error, ref guiRect);
        HierarchyGUIUtility.DrawIconButton(HierarchyGUIUtility.IconType.Warning, ref guiRect);
        HierarchyGUIUtility.DrawIconButton(HierarchyGUIUtility.IconType.CircleRed, ref guiRect);
        HierarchyGUIUtility.DrawIconButton(HierarchyGUIUtility.IconType.DiamondMagenta, ref guiRect);
        HierarchyGUIUtility.DrawIconButton(HierarchyGUIUtility.IconType.CircleYellow, ref guiRect);
        HierarchyGUIUtility.DrawIconButton(HierarchyGUIUtility.IconType.DiamondGrey, ref guiRect);
        HierarchyGUIUtility.DrawIconButton(HierarchyGUIUtility.IconType.Settings, ref guiRect);
    }
}

結果

f:id:halya_11:20190213183649p:plain

関連

light11.hatenadiary.com