Overriding Component Header in Inspector

How Can I override the component header in the inspector? I use UI toolkit / “public override VisualElement CreateInspectorGUI”. So “protected override void OnHeaderGUI” is not called. I want to override the header per component instance so ObjectNames will not work for me. I want to create a dynamic header related to component’s fields.

Component headers are still IMGUI. Quick look in the UI toolkit debugger shows this:

You would have work up the heirarchy, remove said element, and replace it with a UI toolkit version.

As I’ve mentioned before, a lot of what you are trying to do is going to require hacks, experimentation, and some inventiveness. Get creative.

I think header IMGUIContainer controls the inspector layout. When I remove it or override onGUIHandler function inspector is empty.

_root.RegisterCallback<AttachToPanelEvent>(evt =>
{
    var panel = evt.destinationPanel;
    var header = _root.parent.parent.Q<IMGUIContainer>();
    //I tried following 3 lines one by one.
    // Clear does not effect
    //header.Clear();
    //Zero size
    header.onGUIHandler = () => { };
    //Zero size
    //_root.parent.parent.Remove(header);

    //var label = new Label("Test Header");
    //_root.parent.parent.Add(label);
});

Clearing the header won’t do anything as it doesn’t have any children; it’s just an IMGUIContainer.

I managed to figure out where it gets made: UnityCsReference/Editor/Mono/UIElements/Inspector/EditorElement.cs at master · Unity-Technologies/UnityCsReference · GitHub

So you can have fun plugging through what it does to see what the minimum you need to recreate is.