Posts 유니티 - 하이라키 아이콘 적용 예제
Post
Cancel

유니티 - 하이라키 아이콘 적용 예제

Memo


  • NOTE : 아이콘 뿐만 아니라 가능한 GUI를 모두 그릴 수 있다.

image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
[DisallowMultipleComponent]
public class Test_HierarchyIcon : MonoBehaviour
{
#if UNITY_EDITOR
    public static string CurrentFolderPath { get; private set; } // "Assets\......\이 스크립트가 있는 폴더 경로"

    private static Texture2D iconTexture;
    private static string iconTextureFileName = "Icon.png";

    [UnityEditor.InitializeOnLoadMethod]
    private static void ApplyHierarchyIcon()
    {
        InitFolderPath();

        if (iconTexture == null)
        {
            // "Assets\...\Icon.png"
            string texturePath = System.IO.Path.Combine(CurrentFolderPath, iconTextureFileName);
            iconTexture = UnityEditor.AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)) as Texture2D;
        }

        UnityEditor.EditorApplication.hierarchyWindowItemOnGUI += DrawHierarchyIcon;
    }

    private static void InitFolderPath([System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "")
    {
        CurrentFolderPath = System.IO.Path.GetDirectoryName(sourceFilePath);
        int rootIndex = CurrentFolderPath.IndexOf(@"Assets\");
        if (rootIndex > -1)
        {
            CurrentFolderPath = CurrentFolderPath.Substring(rootIndex, CurrentFolderPath.Length - rootIndex);
        }
    }

    // 구버전에서는 GUI.color를 통한 색상 지정이 안되므로, 범용성을 위해 style을 사용한다.
    private static GUIStyle labelStyle;
    static void DrawHierarchyIcon(int instanceID, Rect selectionRect)
    {
        const float Pos = 0f;

        // 1. Icon Rect
        Rect iconRect = new Rect(selectionRect);
        iconRect.x = iconRect.width + Pos;
        iconRect.width = 16f;

        // 2. Label Rect
        Rect labelRect = new Rect(iconRect);
        labelRect.x += 20f; 
        labelRect.width = 60f;

        if (labelStyle == null)
        {
            labelStyle = new GUIStyle(UnityEditor.EditorStyles.label);
            labelStyle.normal.textColor = Color.yellow;
        }

        GameObject go = UnityEditor.EditorUtility.InstanceIDToObject(instanceID) as GameObject;

        if (go != null && go.activeSelf && go.GetComponent<Test_HierarchyIcon>() != null)
        {
            GUI.DrawTexture(iconRect, iconTexture);
            GUI.Label(labelRect, "Nyang", labelStyle);
        }
    }
#endif
}


좌측 끝에 아이콘 하나 띄우기


image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using UnityEngine;

[DisallowMultipleComponent]
public class Test_HierarchyLeftIcon : MonoBehaviour
{
#if UNITY_EDITOR
    public static string CurrentFolderPath { get; private set; } // "Assets\......\이 스크립트가 있는 폴더 경로"

    private static Texture2D iconTexture;
    private static readonly string iconTextureFileName = "Icon.png";

    [UnityEditor.InitializeOnLoadMethod]
    private static void ApplyHierarchyIcon()
    {
        InitFolderPath();

        if (iconTexture == null)
        {
            string texturePath = System.IO.Path.Combine(CurrentFolderPath, iconTextureFileName);
            iconTexture = UnityEditor.AssetDatabase.LoadAssetAtPath(texturePath, typeof(Texture2D)) as Texture2D;
        }

        if (iconTexture != null)
        {
            UnityEditor.EditorApplication.hierarchyWindowItemOnGUI += DrawHierarchyIcon;
        }
    }

    private static void InitFolderPath([System.Runtime.CompilerServices.CallerFilePath] string sourceFilePath = "")
    {
        CurrentFolderPath = System.IO.Path.GetDirectoryName(sourceFilePath);
        int rootIndex = CurrentFolderPath.IndexOf(@"Assets\");
        if (rootIndex > -1)
        {
            CurrentFolderPath = CurrentFolderPath.Substring(rootIndex, CurrentFolderPath.Length - rootIndex);
        }
    }

    static void DrawHierarchyIcon(int instanceID, Rect selectionRect)
    {
        const float Pos =
#if UNITY_2019_3_OR_NEWER      
            32f;
#else
            0f
#endif

        Rect iconRect = new Rect(selectionRect);
        iconRect.x = Pos;
        iconRect.width = 16f;

        GameObject go = UnityEditor.EditorUtility.InstanceIDToObject(instanceID) as GameObject;

        if (go != null && go.GetComponent<Test_HierarchyLeftIcon>() != null)
        {
            GUI.DrawTexture(iconRect, iconTexture);
        }
    }
#endif
}


타입별로 Built-in 아이콘 그려주기


image

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#if UNITY_EDITOR

using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System;

public static class EditorHierarchyIconHelper
{
    [UnityEditor.InitializeOnLoadMethod]
    private static void ApplyHierarchyIcon()
    {
        if (iconData == null || iconData.Length == 0)
            InitIconData();

        UnityEditor.EditorApplication.hierarchyWindowItemOnGUI += DrawHierarchyIcon;
    }

    private static (Type type, Texture icon, Color color)[] iconData;

    private static void InitIconData()
    {
        Texture boxCollider    = EditorGUIUtility.IconContent("d_BoxCollider Icon").image;
        Texture sphereCollider = EditorGUIUtility.IconContent("d_SphereCollider Icon").image;

        iconData = new (Type, Texture, Color)[]
        {
            (typeof(DustManager), EditorGUIUtility.FindTexture("GameManager Icon"), Color.magenta),
            (typeof(Cone),   EditorGUIUtility.FindTexture("d_Favorite On Icon"), Color.yellow),
            (typeof(Camera), EditorGUIUtility.FindTexture("Camera Gizmo"),       Color.cyan),
            (typeof(DustBoxCollider),    boxCollider,    Color.white),
            (typeof(DustSphereCollider), sphereCollider, Color.white),
        };
    }

    private static void DrawHierarchyIcon(int instanceID, Rect selectionRect)
    {
        Rect iconRect  = new Rect(selectionRect);
        iconRect.x     = 32f; // 하이라키 좌측 끝
        iconRect.width = 16f;

        GameObject go = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
        if (go == null)
            return;

        Color c = GUI.color;

        for (int i = 0; i < iconData.Length; i++)
        {
            ref var current = ref iconData[i];
            if (current.icon != null && go.GetComponent(current.type) != null)
            {
                GUI.color = go.activeInHierarchy ? current.color : Color.white * 0.5f;
                GUI.DrawTexture(iconRect, current.icon);
                break;
            }
        }

        GUI.color = c;
    }
}

#endif


참고 : Built-in 아이콘 목록


This post is licensed under CC BY 4.0 by the author.