unity-Hierarchy上显示图集的名称

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

namespace EditorTool
{
    using UnityEngine;
    [InitializeOnLoad]
    class HierarchyTool
    {
        private static Hashtable colorMap;
        //预制颜色表
        private static List preColors;
        static HierarchyTool()
        {
            colorMap = new Hashtable();
            preColors = new List();

            preColors.Add(new Color(255, 0, 255, 0.5f));
            preColors.Add(new Color(0, 255, 255, 0.5f));
            preColors.Add(new Color(255, 255, 0, 0.5f));
            preColors.Add(new Color(0, 255, 0, 0.5f));
            preColors.Add(new Color(255, 0, 0, 0.5f));
            preColors.Add(new Color(0, 0, 255, 0.5f));
            preColors.Add(new Color(255, 51, 153, 0.5f));
            preColors.Add(new Color(204, 255, 0, 0.5f));
            preColors.Add(new Color(255, 102, 0, 0.5f));
            preColors.Add(new Color(153, 102, 0, 0.5f));
            preColors.Add(new Color(51, 153, 153, 0.5f));
            preColors.Add(new Color(152, 102, 153, 0.5f));
            EditorApplication.hierarchyWindowItemOnGUI += hierarchWindowOnGUI;
        }

        private static void hierarchWindowOnGUI(int instanceID, Rect selectionRect)
        {
            Rect rect = new Rect(selectionRect);
            rect.x = rect.x + rect.width - 19;
            rect.width = 18;
            GameObject go  = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
            if (go == null) return;
            //可见性控制
            go.SetActive(GUI.Toggle(rect, go.activeSelf, string.Empty));

            Image image = go.GetComponent();
            if (!image || !image.sprite) return;
            string path = AssetDatabase.GetAssetPath(image.sprite);
            int index = path.LastIndexOf("/");
            if(index>0)
            {
                string sign = "";
                Color color = Color.gray;
                string key = path.Substring(0, index);
                index = key.LastIndexOf("/");
                if (index > 0)
                    sign = key.Substring(index+1).Replace("Atlas_","");
                if (colorMap.Contains(key))
                {
                    color = (Color)colorMap[key];
                }
                else
                {
                    if (preColors.Count > 0)
                    {
                        color = preColors[0]; // new Color(Random.value, Random.value, Random.value, 0.5f);
                        preColors.RemoveAt(0);
                    }
                    else
                    {
                        color = new Color(Random.value, Random.value, Random.value, 0.5f);
                    }
                    colorMap.Add(key, color);
                }

                GUIContent con = new GUIContent(sign);
                rect.width = GUI.skin.GetStyle("Label").CalcSize(con).x;
                rect.x -= rect.width;
                Texture2D tex = EditorGUIUtility.whiteTexture;
                GUI.color = color;
                GUI.DrawTexture(rect, tex);
                GUI.color = Color.white;
                GUI.Label(rect, con);
            }
        }
    }
}

你可能感兴趣的:(unity-Hierarchy上显示图集的名称)