CodeSorcerer
CodeSorcerer

Reputation: 179

Drawing the default inspector with UIElements/UI Builder

I have a LevelEditor script which has a custom inspector editor built with UIBuilder and UIElements. The problem is that i would like to cache references inside a list but i do not want to make a custom list view for it (it's kinda hard to implement in UIElements and not worth the time) so i thought i would just make the private List<MyType> _myVar; as [Serializefield] and then draw the DefaultInspector below the custom one.

I found a method that does this DrawDefaultInspector but unfortunately it does nothing.

public override VisualElement CreateInspectorGUI()
{
    _visualTree.CloneTree(_rootElement);

    // My custom editor code
    ...

    DrawDefaultInspector();

    return _rootElement;
}

Any help is appreciated...

Upvotes: 5

Views: 2940

Answers (2)

E. Zacarias
E. Zacarias

Reputation: 647

Be careful, the accepted answer showing the trick with DrawDefaultInspector seems to be obsolete in the newer versions¹ of Unity, which use UIElements by default in the inspector window.

The standard way to insert the default inspector is by populating a VisualElement with the controls. The code below show how you do that:

using UnityEngine.UIElements;
using UnityEditor.UIElements;

...

VisualElement defaultInspector = new VisualElement();
InspectorElement.FillDefaultInspector(defaultInspector, serializedObject, this);
// "this" is the Editor itself

Now you have a populated defaultInspector element which you can return as the root element, add elements to it, or add it to the root element. I myself would recommend to add it to the root element, but I have no strong reason for that.

¹I am not sure which version started to use it, or how is it implemented internally by now, but the official docs recommends this way starting from 2021.3.

Upvotes: 3

Menyus777
Menyus777

Reputation: 7007

You need to place an IMGUIContainer via UIBuilder (or by code via _rootElement.Add(IMGUIVisualElement)) and than do the following inside CreateInspectorGUI

public override VisualElement CreateInspectorGUI()
{
    _visualTree.CloneTree(_rootElement);

    // Your custom editor code
    ...

    // Drawing the default inspector
    var defaultInspector = _rootElement.Q<IMGUIContainer>();
    defaultInspector.onGUIHandler = () => DrawDefaultInspector();

    return _rootElement;
}

What i usually do is placing the DefaultInspector in a foldout too so designers can use the custom Inspector but whenever they need some advanced stuff that ain't visible to them they can use the default inspector.

So a layout like this will appear to them (default inspector is hidden by default ofc :) ) enter image description here

Upvotes: 5

Related Questions