Hello. I am playing about with editor code, and was wondering how easy it is to add a new button and a small readout to the top bar of the hierarchy window?
I’ve managed to do it for items in the window using EditorApplication.hierarchyWindowItemOnGUI but I can’t seem to find a way to do it for the window itself. Is there any way?
[InitializeOnLoad]
public class CustomHierarchy
{
static CustomHierarchy()
{
// Get Window Type
var type = typeof(EditorWindow).Assembly.GetType("UnityEditor.SceneHierarchyWindow");
var window = EditorWindow.GetWindow(type, false, "Hierarchy", false);
// Delay call to prevent NULL ref
EditorApplication.delayCall += () =>
{
var rootVisualContainer = window.rootVisualElement;
// Offset container to give space for our Toolbar
rootVisualContainer.style.top = new StyleLength(45);
var toolbar = new UnityEditor.UIElements.Toolbar();
// Add "defaultcommondark_inter.uss" StyleSheet to our Toolbar so it can render correctly
toolbar.styleSheets.Add(rootVisualContainer.styleSheets[0]);
toolbar.StretchToParentSize();
toolbar.style.position = new StyleEnum<Position>(Position.Absolute);
// Shift toolbar to be under docker element
toolbar.style.top = new StyleLength(24);
toolbar.style.marginLeft = 1;
var toolbarButton = new ToolbarButton();
toolbarButton.text = "Button";
toolbarButton.style.width = 100;
toolbar.Add(toolbarButton);
var editorPanelRootElement = rootVisualContainer.parent;
editorPanelRootElement.Insert(1, toolbar);
};
}
}
That will still nullref if the user doesn’t have a Hierarchy tab at all. Otherwise it’s great, and a very good example of why UITK is a great addition to the engine.
Ok, after some testing old code had many issues. Mostly about how after some manipulations with window it was resetted to the old state (Repositioning of elements).