Display Game Object's layer name in Hierarchy

· 1 min read
Display Game Object's layer name in Hierarchy

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