Decorating a hierarchy view

One of the cool things with Unity is that Editor classes expose a ton of callbacks for plugging your own logic into.

One of these things is the EditorApplication.hierarchyWindowItemOnGUI event (which is a publish-subscribe) way to connect your event handler (or multiple handlers):

EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyWindowItemOnGUI;

Now, the method OnHierarchyWindowItemOnGUI receives two parameters:

private void OnHierarchyWindowItemOnGUI(int instanceid, Rect selectionrect)
{
// rendering stuff here
}

The first one is the instance ID (of the game object in the hierarchy), while the second one is the Rect used by the hierarchy node to render.

You can use this Rect to render the custom stuff over the node label. With a little math you can position it precisely, as you like.

After each hierarchy change – with the help of reflection – I’m walking the tree and pulling out data about the attached scripts.

This allows me to render various icons over the hierarchy view:

  • eDriven.Gui component adapter (blue dot)
  • style mappers (red dot)
  • font mappers (green dot)
  • audio player mappers (orange dot)
  • attached scripts with methods having the event handler signature (lightning) – when clicked automatically switches to Events view

This helps visually to have things a bit more organized. :)

hierarchy1

hierarchy2

Posted in News