Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


BindViewItem

Raised when a HierarchyViewItem is bound to a HierarchyView. Use this event to customize the view item.
Read time 2 minutesLast updated 10 days ago

Definition

public static event HierarchyWindow.BindViewItemEventHandler BindViewItem

Remarks

The following example changes the icon a GameObject uses in the Hierarchy window if it has a specified tag. It uses
BindViewItem
to register a handler that customizes each item as the window binds it, and applies a custom USS icon class to GameObjects with the
Favorite
tag.
The example requires a USS file called
ChangeNodeIcon.uss
and a tag called
Favorite
.
To use this example:
  1. Save the script in a folder called
    Assets/Editor/ChangeNodeIcon
    . Scripts in an
    Editor
    folder can use the Hierarchy module API without additional setup. If you save the script outside of an
    Editor
    folder, you must enable the Hierarchy built-in module in the Package Manager window, which also adds the module to your Player builds.
  2. Copy the styles from the USS example on this page. Save them in a USS file called
    ChangeNodeIcon.uss
    in the same
    Assets/Editor/ChangeNodeIcon
    folder.
  3. Create a tag called
    Favorite
    : select a GameObject, open the Tag dropdown in the Inspector window, and select Add Tag.
  4. Assign the
    Favorite
    tag to a GameObject to change the icon it displays.
using Unity.Hierarchy;using Unity.Hierarchy.Editor;using UnityEditor;using UnityEngine;using UnityEngine.UIElements;namespace Unity.HierarchySamples.Editor{ class ChangeNodeIcon { const string k_FavoriteUssClass = "gameobject--favorite"; [InitializeOnLoadMethod] static void Initialize() { HierarchyWindow.BindView += OnBindView; HierarchyWindow.BindViewItem += OnBindViewItem; } static void OnBindView(HierarchyWindow window, HierarchyView view) { view.styleSheets.Add(AssetDatabase.LoadAssetAtPath<StyleSheet>("Assets/Editor/ChangeNodeIcon/ChangeNodeIcon.uss")); } static void OnBindViewItem(HierarchyWindow window, HierarchyView view, HierarchyViewItem item) { if (item.Handler is not HierarchyGameObjectHandler handler) return; GameObject gameObject = handler.GetGameObject(item.Node); if (gameObject == null) return; // CompareTag throws an error if the 'Favorite' tag isn't defined in the project. // To define the 'Favorite' tag, select a GameObject, open the Tag dropdown in the Inspector window, select Add Tag, and create a new tag called 'Favorite'. if (System.Array.IndexOf(UnityEditorInternal.InternalEditorUtility.tags, "Favorite") < 0) return; bool isFavorite = gameObject.CompareTag("Favorite"); item.Icon.EnableInClassList(k_FavoriteUssClass, isFavorite); // Inline styling needs to be cleared to show custom USS icon. if (isFavorite) item.Icon.style.backgroundImage = StyleKeyword.Null; } }}
The following example shows how to style
ChangeNodeIcon.uss
.
.hierarchy-item__icon.gameobject--favorite { background-image: resource("Icons/Favorite_colored.png");}.unity-collection-view__item--selected:hover:enabled .hierarchy-itemicon.gameobject--favorite,.unity-collection-view:focus .unity-collection-viewitem--selected .hierarchy-itemicon.gameobject--favorite { background-image: resource("Icons/Favorite_colored.png");}