效果

unity编辑器Hierarchy添加图标

 

素材

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);
        }
    }

}

 

相关文章:

  • 2021-11-29
  • 2021-06-11
  • 2021-11-30
  • 2021-08-09
  • 2021-07-29
  • 2021-07-16
  • 2021-11-23
  • 2021-06-04
猜你喜欢
  • 2022-12-23
  • 2021-04-06
  • 2022-12-23
  • 2021-08-13
  • 2022-12-23
  • 2021-10-31
  • 2021-04-29
相关资源
相似解决方案