Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
6
Question by nuverian · Apr 04, 2013 at 11:13 PM · editorhierarchyeditorguiiconcallback

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

5 Replies

· Add your reply
  • Sort: 
avatar image
19
Best Answer

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!

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Partel-Lang · May 15, 2014 at 10:15 AM 2
Share

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 :(

avatar image GerryM · Jun 16, 2014 at 08:24 PM 0
Share

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!

avatar image C2 Game Studio · Dec 13, 2014 at 04:25 PM 0
Share

The code works but was killing my game in editor and editor performance in general. There has to be a more efficient way.

avatar image C2 Game Studio · Dec 13, 2014 at 05:09 PM 0
Share

Removing the UpdateCB subscription and just doing the get component on HierarchyItemCB improved performance vastly.

avatar image Heatith · Mar 31, 2015 at 01:15 AM 0
Share

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.

Show more comments
avatar image
11

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


Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Demigiant · Jul 12, 2016 at 12:47 PM 1
Share

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.

avatar image correia55 Demigiant · Aug 23, 2017 at 08:55 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image nuverian · Apr 05, 2013 at 02:35 PM 0
Share

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

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Stardog · Oct 19, 2020 at 06:44 PM

https://forum.unity.com/threads/free-hierarchy-window-icons-on-objects-via-interfaces.436548/

https://assetstore.unity.com/packages/tools/utilities/cghierarchyicons-163353

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom icons for files on Project Window 0 Answers

What is the best way to draw icons in Unity's Hierarchy window? 1 Answer

Initialising List array for use in a custom Editor 1 Answer

Unity Hierarchy never saves order 1 Answer

What are these blue icons over assets in the unity editor? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges