Create a default Inspector
Learn how to create a default Inspector by an example.
Read time 2 minutesLast updated a month ago
Default Inspector is a standard way to display serialized object properties without any custom modifications. When you use FillDefaultInspector method of the InspectorElement class, it automatically creates the default hierarchy with the default property fields.
Example overview
This example extend the method in the Create a Custom Inspector example to create a default Inspector for the component. The example creates a control in the file and attaches the default Inspector UI to it.
CreateInspectorGUI()CarFoldoutCar_Inspector_UXML.uxmlPrerequisites
This guide is for developers familiar with the Unity Editor, UI Toolkit, and C# scripting. Before you start, get familiar with the following:
Create the default Inspector container
Create a Foldout control to display the default Inspector UI.
-
Double-click thefile to open it in UI Builder.
Car_Inspector_UXML.uxml -
Add a Foldout control to your UI, name it, and set a label text:
Default_Inspector
Foldout for the default Inspector
Attach the default Inspector UI
To attach the default Inspector UI to the Foldout, you must obtain a reference to it. You can retrieve the visual element of the Foldout from the visual tree of your Inspector using UQuery, and use the FillDefaultInspector method of the InspectorElement class to attach the default Inspector UI to the Foldout control.
-
In thefile, update the
Car_Inspector.csmethod to get a reference to theCreateInspectorGUI()Foldout and attach the default Inspector UI to it:Default_Inspectorpublic override VisualElement CreateInspectorGUI(){ ... // Get a reference to the default Inspector Foldout control. VisualElement InspectorFoldout = myInspector.Q("Default_Inspector"); // Attach a default Inspector to the Foldout. InspectorElement.FillDefaultInspector(InspectorFoldout, serializedObject, this); // Return the finished Inspector UI. return myInspector;} -
Select the GameObject that has thecomponent to it. The Inspector for the
Carcomponent now displays thecarFoldout with the default Inspector UI inside.Default Inspector
Inspector with a default Inspector