Is there an easy way to draw the default inspector for a target object inside an EditorWindow or to get it back as a VisualElement?
Didn’t you ask this also in scripting area yesterday? Did you try what I recommended, or is it something else you are looking for than CreateEditor?
This is specifically for UI elements.
I did find this but haven’t tried it yet.
https://docs.unity3d.com/2019.1/Documentation/ScriptReference/UIElements.InspectorElement.html
The InspectorElement will not always use UIElements to draw the inspector. By default, we still use the IMGUI default inspector. You have to recreate the PropertyFields yourself for now. Here’s a very helpful user that shared some code to do this:
This worked great, thanks for pointing me to it. InspectorElement worked too, but having everything in UIElements when possible is better IMO.
Is there a reason that only some of the Attributes are rendered with this approach? For instance [Range()] will display correctly, but [Header()] and [Space()] will not.
UIElements does not yet implement the various property drawing attributes you can add on your fields to display them differently. They will all draw using IMGUI. This is just missing functionality, not a technical limitation. You can actually implement some of them yourself if you wish - at least until we have some official ones out.
I figured this was probably the case. Thanks for the info!
Try this, it work for me ![]()
Editor editor = Editor.CreateEditor(component);
IMGUIContainer inspectorIMGUI = new IMGUIContainer(() => { editor.OnInspectorGUI(); });
I ended up drawing the properties individually by iterating through the SerializedObject.
I should probably have supported drawing the Editor, since now PropertyDrawers work but CustomEditors will not work. However I needed some things like the missing decorators and stuff to work so for now thats what it is. I’ll probably spend some time to find a better way later.
It did turn out to work just fine and mimics the inspector just fine which is what I wanted for this database item editor.
There are a couple of bugs I found on the way, though. For instance Disabled field references (m_script here) apparently can’t be clicked to find the script reference asset.
Heres the relevant code.
private static void BuildInspectorProperties(SerializedObject obj, VisualElement container)
{
// TODO [Header()] and [Space()] are manually added until Unity supports them.
SerializedProperty iterator = obj.GetIterator();
Type targetType = obj.targetObject.GetType();
List<MemberInfo> members = new List<MemberInfo>(targetType.GetMembers());
if (!iterator.NextVisible(true)) return;
do
{
PropertyField propertyField = new PropertyField(iterator.Copy())
{
name = "PropertyField:" + iterator.propertyPath
};
MemberInfo member = members.Find(x => x.Name == propertyField.bindingPath);
if (member != null)
{
IEnumerable<Attribute> headers = member.GetCustomAttributes(typeof(HeaderAttribute));
IEnumerable<Attribute> spaces = member.GetCustomAttributes(typeof(SpaceAttribute));
foreach (Attribute x in headers)
{
HeaderAttribute actual = (HeaderAttribute) x;
Label header = new Label { text = actual.header};
header.style.unityFontStyleAndWeight = FontStyle.Bold;
container.Add(new Label { text = " ", name = "Header Spacer"});
container.Add(header);
}
foreach (Attribute unused in spaces)
{
container.Add(new Label { text = " " });
}
}
if (iterator.propertyPath == "m_Script" && obj.targetObject != null)
{
propertyField.SetEnabled(value: false);
}
container.Add(propertyField);
}
while (iterator.NextVisible(false));
}
And it working in action…

Sorry to resurrect this thread, but I have a question here – when you say it still uses the IMGUI default inspector by default, do you mean that:
- Custom Editors that use UIElements would not use IMGUI? or
- There’s a way to change the default behavior to stop using IMGUI?
EDIT: Just did some quick editing and saw that implementing my own edtior’s CreateInspectorGUI() resulted in the UIElements verison being called!
Simple, best solution until now. Thank you for sharing.
Since this is the first result on Google, this is how to populate the default inspector in UI Toolkit:
InspectorElement.FillDefaultInspector(yourContainer, this.serializedObject, this);
As seen here under Car_Inspector.cs.
Also, why pass in both this and this.serializedObject??
Hi. I noticed populating the default inspector either with the new UIToolkit way or the old DrawDefaultInspector in OnInspectorGUI() way still yields a non-styled version of the gui (see img) when I am overriding the GameObject editor

but I want to retain the default inspector styling like so (after which I want to add my own element)

Is there any way to accomplish this?
I don’t know if there’s a UITK solution for that, but you can use the Editor. finishedDefaultHeaderGUI with IMGUI for that.
We don’t have a UI Toolkit version of this yet.
Sweet, thanks!
Thanks!
Worked for me like a charm. Thank you very much.