unity编辑器Hierarchy添加图标

效果

 

素材

 

 

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

[InitializeOnLoad]
class MyHierarchyIcon
{
    static Texture2D texture;
    static List<int> markedObjects;

    //静态构造
    static MyHierarchyIcon()
    {
        //需要自己准备一张图放到如下路径  Assets/Images/Testicon.png
        texture = AssetDatabase.LoadAssetAtPath("Assets/Images/Testicon.png", typeof(Texture2D)) as Texture2D;
        EditorApplication.update += UpdateCB;
        EditorApplication.hierarchyWindowItemOnGUI += HierarchyItemCB;
    }

    static void UpdateCB()
    {
        // Check here
        GameObject[] go = Object.FindObjectsOfType(typeof(GameObject)) as GameObject[];

        markedObjects = new List<int>();
        foreach (GameObject g in go)
        {
            // Example: mark all lights  判断放图标的条件,比如有灯关组件
            if (g.GetComponent<Light>() != null)
                markedObjects.Add(g.GetInstanceID());
        }

    }

    static void HierarchyItemCB(int instanceID, Rect selectionRect)
    {
        // place the icoon to the right of the list:
        Rect r = new Rect(selectionRect);
        r.x = r.width - 10;//图片位置
        r.width = 16;//图片宽度

        if (markedObjects.Contains(instanceID))
        {
            // Draw the texture if it's a light (e.g.)
            GUI.Label(r, texture);
        }
    }

}

 

posted @ 2019-07-30 18:04  三页菌  阅读(1078)  评论(0编辑  收藏  举报