The Hierarchy window in Unity is a placeholder of every GameObject in the current Scene. Information such as whether the game object is a clone of a prefab and whether the game object is inactive can be derived solely based on the hierarchy window without clicking on each game object in the hierarchy and looking at its inspector. Unfortunately, the design is as primitive as it can be. You can see that from the screenshot below.
The prefab game object will be displayed with a blue color font while a game object that is not a prefab will be shown with the default black color font and inactive game objects are shown with a lighter color tone font.
In my case, I could not even notice the differences because my monitor has difficulty showing the contrast!
So I decided to make the hierarchy better for my benefit and share it with you all for yours too.
At the end of this, you should be able to have your hierarchy looking at the below. (I have named the game objects to show what type of game object it is representing)
I will also throw in an editor so that you can change the colors if my taste in that does not suit you!
Core Code: Crediting Unity3Dcollege
Bulk of the codes here are from Unity3DCollege available in video walkthrough on youtube. What I will do is to tell about my differences and the rest is to check out the video tutorial from Jason of Unity3DCollege who gave a very detailed explanation of the code. In short:
The code has a InitializeOnLoad to run the static functions in the class immediately once the Unity Editor is available.
The CustomHierarchy is to run the HandleHierarchyWindowItemOnGUI function at the OnGUI events of all items in the Hierarchy Window.
The HandleHierarchyWindowItemOnGUI static function is the place where we put our code to modify the hierarchy items.
Differentiating the prefab instance: Crediting Unity3Dcollege
Same as the Core Code, bulk of this code are from the Unity3DCollege youtube video tutorial so check out the tutorial for the explanation. In summary, it is to check if the item at the Hierarchy is a prefab. If it is, set it with a black color bold font.
Differentiating a prefab instance that has changes
Now on to my changes. To differentiate out a prefab instance that has changes from its prefab, we will use the PrefabUtility.GetPropertyModifications to get all the modifications done to the instance. We will need to filter out some of the changes which will occur when you create a prefab instance(i got those through trial and error). Any more modifications will mean that it is a prefab instance which has changed. If it has changed, then we will set its font to white.
Note that I break out from the foreach loop once I found a difference. This is to reduce the strain on Unity running through every modification. I used this on a prefab instance which have a lot of changes(because it has a lot of child game objects which have differences). My Unity Editor was slowing down to the point that I need to force close the editor and remove the script from the File Explorer
Differentiating an inactive Game Object
This is the easiest of the lot. What we need to do is to get the Game Object that the item in Hierarchy is referring to and check if it is inactive. If it is, we will set a green highlight on the item in the hierarchy.
Because we are adding the DrawRect function with a new background color. We will need to declare a default background color so that we will not see any anomaly trying to set a null background color. We also need it to set the default selection background color when we are selecting an item. These are all part of the Youtube tutorial of Unity3DCollege.
Preparation for Editor
To create an editor for changing the colors on the fly, we will need to declare some public static variables. To be specific, these will be color variables which will be used to update the color. They will default to their original color.
Merging all together
That is all for the code walkthrough. Below is the full code for the Custom Hierarchy. Next up will be creating the Editor.
Editor Code
Code for the editor is quite straightforward because of the prep works we did earlier.
Enjoy fruits of your labor
Now you can launch your new editor window from Tools->Hierarchy Editor.
You can, of course, adapt it further to add more color to differentiate different game object types.
Hi, I am Yarn Then Cohos King. Welcome to my blog. I am someone who always look to find more effective or efficient ways of gettings things done. Now I am sharing these as well as my thoughts and ideas to benefit others via this blog.
You can contact me via:
Email
or contacting me via the Guestbook