- Home /
How to show an icon in Hierarchy view
Hi,
Like the titles says, I want to know how it's possible to show an icon next to a gameobject in the hierarchy/scene view, considering that the gameobject has a specific monoBehaviour attached to it.
Thanks a lot
Answer by GerryM · Apr 22, 2013 at 07:40 PM
The documentation is a bit lean on this topic, but it's possible by adding your callback to hierarchyWindowItemOnGUI.
There you can draw custom GUI stuff for each hierarchy element.
To mark only certain items, you can use a list in the update callback. For example, you can check your items if they do contain a certain script. I did check for lights in the example.
Here's the code:
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
[InitializeOnLoad]
class MyHierarchyIcon
{
static Texture2D texture;
static List<int> markedObjects;
static MyHierarchyIcon ()
{
// Init
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 - 20;
r.width = 18;
if (markedObjects.Contains (instanceID))
{
// Draw the texture if it's a light (e.g.)
GUI.Label (r, texture);
}
}
}
Furthermore, you can do the same in the project window via projectWindowItemOnGUI.
Happy scripting!
Thanks for sharing, but this is just terrible on the performance. You are doing a GetComponent call on every single game object in the scene and thats just for a single icon. It would be much more efficient to do Object.FindObjectsOfType(typeof(Light)) ins$$anonymous$$d. But it still gets quite slow I bet :(
Good point, indeed. However, I won't spend too much time on optimizing editor scripts, as they usually run on high end machines anyways. Also, this is just intended to show the concept.
But of course, optimization could be an issue. Thanks for noting!
The code works but was killing my game in editor and editor performance in general. There has to be a more efficient way.
Removing the UpdateCB subscription and just doing the get component on HierarchyItemCB improved performance vastly.
Sorry to bump this topic after so long but I don't think you have to remove UpdateCB. Just replace EditorApplication.update with EditorApplication.hierarchyWindowChanged.
I'm not sure, I'm just starting to learn Unity and C#, but I haven't noticed any hit on performance by using it. I thought it might be useful.
Answer by laurentlavigne · Dec 16, 2015 at 10:50 AM
If you want a fast version (0.03ms vs 6ms) try this:
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;
[InitializeOnLoad]
class MyHierarchyIcons
{
static Texture2D texturePanel;
static List<int> markedObjects;
static MyHierarchyIcons ()
{
// Init
texturePanel = AssetDatabase.LoadAssetAtPath ("Assets/art/icons/icon ui panel.psd", typeof(Texture2D)) as Texture2D;
EditorApplication.hierarchyWindowItemOnGUI += HierarchyItemCB;
}
static void HierarchyItemCB (int instanceID, Rect selectionRect)
{
// place the icon to the right of the list:
Rect r = new Rect (selectionRect);
r.x = r.width - 20;
r.width = 20;
GameObject go = EditorUtility.InstanceIDToObject (instanceID) as GameObject;
if (go.GetComponent<UIPanel>())
GUI.Label (r, texturePanel);
}
}
Still doing a lot of possibly useless GetComponents when navigating the Hierarchy. I would recommend grabbing the list of objects inside an EditorApplication.hierarchyWindowChanged callback ins$$anonymous$$d.
If I understand you correctly what you suggest is similar to the selected "Best Answer" but ins$$anonymous$$d of using EditorApplication.update, using EditorApplication.hierarchyWindowChanged. I've tried this and the problem is that the instanceIDs are different for the same object in the when you create the list and after when you check for it, so it doesn't work. Please let me know if I misunderstood you or did something wrong.
Answer by Loius · Apr 05, 2013 at 12:41 AM
I don't know that there's a way to show icons in Hierarchy or Project, but in Scene view it's simple:
function OnDrawGizmosSelected() {
Gizmos.DrawIcon ( transform.position, "NodeIcon.tiff" );
}
The texture has to be in the Assets/Gizmos folder.
Hi thanks. Ye I knew about the gizmos and the scene view. I am wondering if there is a way to show in the hierarchy panel itslef.Thanks
Answer by dreammakersgroupAdmin · Apr 22, 2013 at 06:08 AM
vote for this vote for this http://feedback.unity3d.com/unity/all-categories/1/hot/active/add-onhierarchyviewgui-to-draw-g