You've successfully subscribed to Hacking with Unity - Learn how to create your own games
Great! Next, complete checkout for full access to Hacking with Unity - Learn how to create your own games
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.
Success! Your billing info is updated.
Billing info update failed.

Display Game Object's layer name in Hierarchy

Ben Liu
Ben Liu

It is especially useful when we have numerous game objects in the Hierarchy, the following is one example of showing game object's layer name:

Note that Default, Player, UI are the layer names.

To achieve above function is quite simple, all you need is to create a script and put into the project.

using UnityEditor;
using UnityEngine;

[InitializeOnLoad]
public class ShowLayerNames {

    static readonly int IgnoreLayer = LayerMask.NameToLayer("Default");

    static ShowLayerNames() {
        EditorApplication.hierarchyWindowItemOnGUI += HandleHierarchyWindowItemOnGUI;
    }

		static readonly GUIStyle _style = new GUIStyle() {
        fontSize = 12,
        alignment = TextAnchor.MiddleRight
    };


    static void HandleHierarchyWindowItemOnGUI(int instanceID, Rect selectionRect) {
        var gameObject = EditorUtility.InstanceIDToObject(instanceID) as GameObject;

        if (gameObject != null) {
            EditorGUI.LabelField(selectionRect, LayerMask.LayerToName(gameObject.layer), _style);
        }
    }
}

[InitializeOnLoad]: means scripts will be running when Unity Editor starts, you can do the similar function by my other post: How to run unattached script in Unity

EditorApplication.hierarchyWindowItemOnGUI: two parameters, first one is the object instance id, the second one is the display area.

Reference

https://docs.unity3d.com/ScriptReference/EditorApplication.HierarchyWindowItemCallback.html

Ben Liu

Please feel free to contact me if you have any question. <br> https://t.me/NeedoneApp