Embed UI Toolkit Editor Inside Another UI Toolkit Editor?

I have a GameObject with a reference to a ScriptableObject that has a UI Toolkit Editor. The goal is to draw that Editor if the ScriptableObject is assigned. With IMGUI I used this approach:

[CustomEditor(typeof(MyClass))]
class MyEditor : Editor
{
    MyClass myClass;

    private void OnEnable()
    {
        myClass = (MyClass)target;
    }
    public override void OnInspectorGUI()
    {
        Editor editor = CreateEditor(myClass.scriptable);
        editor.OnInspectorGUI();
    }
}

With UI Toolkit I tried this, but the Editor seems detached from the ScriptableObject:

[CustomEditor(typeof(MyClass))]
class MyEditor : Editor
{
    MyClass myClass;

    private void OnEnable()
    {
        myClass = (MyClass)target;
    }
    public override VisualElement CreateInspectorGUI()
    {
        var root = new VisualElement();

        var editor = CreateEditor(myClass.scriptable);
        root.Add(editor.CreateInspectorGUI());

        return root;
    }
}

All of the values are default values and no changes are saved. Any idea on how to get this to work?

Calling CreateInspectorGUI() will only create the ui, without binding it.

What you can try:

var editor = CreateEditor(myClass.scriptable);
var inspector = editor.CreateInspectorGUI();
root.Add(inspector);
inspector.Bind(editor.serializedObject);

return root;

Usually you don’t need to Bind because the InspectorElement does it for you.

Depending on your editor version, YMMV, as there were a few related issues that were fixed in the past. Let me know if that helps

2 Likes

Just tested it and it worked! Thanks for the help.

1 Like