Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Free] Hierarchy Window Icons on objects via Interfaces

Discussion in 'Scripting' started by LaneFox, Oct 16, 2016.

  1. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    NOTE: this is pretty old now and there's probably way better ways to do this or even good free ones floating around by now.

    I spent a bit of time trying to figure out how to do this and figured it should be a little more standard and easier to do so I'm posting it here for everyone to use and mess with.

    Basically you can hook into the window, check if each object being displayed has a component, then draw the proper icon for that component.

    This method uses Interfaces, but you can do it however you want obviously. It definitely has drawbacks, but I just wanted to get something going quick and not worry about it so if you feel compelled to improve it, go ahead.
    • Main hook/eval/draw script
    • Interface script
    • Implementation code
    Results
    shot_161015_175113.png

    HierarchyIcons.cs

    Code (csharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3.  
    4.     [InitializeOnLoad]
    5.     public class HierarchyIcons
    6.     {
    7.         static HierarchyIcons() { EditorApplication.hierarchyWindowItemOnGUI += EvaluateIcons; }
    8.  
    9.         private static void EvaluateIcons(int instanceId, Rect selectionRect)
    10.         {
    11.             GameObject go = EditorUtility.InstanceIDToObject(instanceId) as GameObject;
    12.             if (go == null) return;
    13.  
    14.             IHierarchyIcon slotCon = go.GetComponent<IHierarchyIcon>();
    15.             if (slotCon != null) DrawIcon(slotCon.EditorIconPath, selectionRect);
    16.         }
    17.  
    18.         private static void DrawIcon(string texName, Rect rect)
    19.         {
    20.             Rect r = new Rect(rect.x + rect.width - 16f, rect.y, 16f, 16f);
    21.             GUI.DrawTexture(r, GetTex(texName));
    22.         }
    23.  
    24.         private static Texture2D GetTex(string name)
    25.         {
    26.             return (Texture2D)Resources.Load("Icons/" + name);
    27.         }
    28.     }
    IHierarchyIcon.cs
    Code (csharp):
    1.     public interface IHierarchyIcon
    2.     {
    3.         string EditorIconPath { get; }
    4.     }
    Implementation
    Code (csharp):
    1. {...}
    2.     public class ItemUiSlot : MonoBehaviour, IHierarchyIcon
    3.     {
    4.         public string EditorIconPath { get { return "LogoGrey";  } }
    5. {...}
     

    Attached Files:

    Last edited: Oct 19, 2020
    Stardog, mitaywalle, vedram and 4 others like this.
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Someone asked how to make the objects highlighted. Just use the rect you get from the callback and fill a texture into it. Again, this can be much better with cached colors/textures etc but this is just an example. You should also probably draw the highlight effect before the icon texture so it doesn't fade it out.

    HierarchyIcons.cs (one modified method)
    Code (csharp):
    1.         private static void DrawIcon(string texName, Rect rect)
    2.         {
    3.             Rect r = new Rect(rect.x + rect.width - 16f, rect.y, 16f, 16f);
    4.             GUI.DrawTexture(r, GetTex(texName));
    5.  
    6.             Texture2D t = new Texture2D(1, 1);
    7.             Color c = new Color(100, 200, 100, 0.1f);
    8.             t.SetPixel(1, 1, c);
    9.             t.Apply();
    10.             GUI.DrawTexture(rect, t, ScaleMode.StretchToFill);
    11.         }
    shot_161017_090721.png
     
  3. leodluz

    leodluz

    Joined:
    Jul 12, 2016
    Posts:
    17
    What is IDropHandler?
    upload_2017-3-3_20-13-1.png
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Just a byproduct of the implementation example. It's not part of doing the hierarchy icons.
     
    paulbuck86 likes this.
  5. paulbuck86

    paulbuck86

    Joined:
    Apr 17, 2017
    Posts:
    15
    This is amazing, going to play around with this.
     
    LaneFox likes this.