Custom Inspector button

I’m new to this UI-Toolkit stuff, so, it’s brobably a very noob question

I’m trying to make an custom inspector that has a button, and whenever I click on it will call an function, but it’s not happening at all :frowning:

Here’s some screenshots:

8681733--1170465--upload_2022-12-22_17-41-12.png

And the code:

(contained in a [ExecuteInEditMode] Monobehaviour class)

public void OnEnable()
    {
        var root = GetComponent<UIDocument>().rootVisualElement;
        Button setPrefabButton = root.Q<Button>("set-prefab");
        setPrefabButton.RegisterCallback<ClickEvent>(ev => { print("AAAA"); }); //Not working
        setPrefabButton.clicked += () => { print("AAAA"); }; //Not working
    }

I did some tests and it detects when the mouse hovers the button.
It does finds the button, if I try to debug.log the “setPrefabButton” it don’t returns me null.
OnEnable() is happening.

EDIT:
Unity 2022.1.0f1

8681733--1170456--upload_2022-12-22_17-36-26.png

UIDocument components are for runtime UI, not for inspectors. I recommend reading this: Unity - Manual: Create a Custom Inspector

Oh, okay, but there’s no way to make an interactable Inspector button with ui-toolkit?

Yes there is. And it works pretty similar to what you are already doing. You’ve got to make a custom Editor. You must already be making a custom Editor to even show the button there, aren’t you?

There are multiple steps and I don’t know which ones you don’t know. You already know how to use a UITK button; all the other steps are explained there on the docs.

I understand that’s something really basic I’m missing here.
The best I found was these samples which seems to work in Inspector:

8683824--1170864--upload_2022-12-23_15-51-7.png

But idk what ‘container’ is if it’s not “UIDocument”

A container in this context can be any good old VisualElement. Did you check the link I posted?