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;
        //預(yù)制顏色表
        private static List<Color> preColors;
        static HierarchyTool()
        {
            colorMap = new Hashtable();
            preColors = new List<Color>();

            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<Image>();
            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);
            }
        }
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容