Create a custom Inspector
Create a custom Inspector for a MonoBehaviour or ScriptableObject class using C# scripts and UI Builder.
Read time 10 minutesLast updated 2 months ago
Version: 2022.3+
Although there is a default Inspector for your MonoBehaviours and ScriptableObjects, you might want to write a custom Inspector for your MonoBehaviour and ScriptableObject classes. Custom Inspectors only work for types that derive from MonoBehaviour or ScriptableObject. If you have a plain serializable class (for example, one used inside an array or list), use a custom property drawer instead. For more information, refer to Create a custom property drawer. A custom Inspector can help you do the following:
- Create a more user-friendly representation of script properties.
- Organize and group properties together.
- Display or hide sections of the UI depending on the user's choices.
- Provide additional information about the meaning of individual settings and properties.
Example overview
This example creates a custom Inspector for a MonoBehaviour class. It uses both C# scripts and UI Builder to create the UI. The custom Inspector also features a custom property drawer.
The custom Inspector displays the properties of a class, including the make, year built, color, and a list of tires. The Inspector uses a PropertyField control to display the properties of the class, and a custom property drawer to display the properties of the class.
CarCarTireYou can find the completed files that this example creates in this GitHub repository.
Prerequisites
This guide is for developers familiar with the Unity Editor, UI Toolkit, and C# scripting. Before you start, get familiar with the following:
Create a new MonoBehaviour
To create a custom Inspector, first define a custom class that inherits from a . The custom class represents a simple car with several properties.
MonoBehaviour-
Create a project in Unity with any template.
-
In your Project window, create a folder namedto store all your files.
create-a-custom-inspector -
Create a C# script namedwith the following content:
Car.csusing UnityEngine;public class Car : MonoBehaviour{ public string make = "Toyota"; public int yearBuilt = 1980; public Color color = Color.black;} -
Create a new GameObject in the scene and attach thescript component to it.
Car
Default Inspector for the Car object
Create a custom Inspector script
To create a custom Inspector for any serialized object, you need to create a class deriving from the Editor base class and add the CustomEditor attribute to it. This attribute lets Unity know which class this custom Inspector represents.
-
Create a folder namedinside the
Editorfolder.create-a-custom-inspector -
Create a C# script namedinside the
Car_Inspector.csfolder with the following content:Editorusing UnityEditor;using UnityEditor.UIElements;using UnityEngine.UIElements;[CustomEditor(typeof(Car))]public class Car_Inspector : Editor{} -
Select the GameObject that has thecomponent attached to it. The default Inspector displays.
Car -
To replace the default Inspector, inside theclass, add the following code to override CreateInspectorGUI() and return a new visual element containing the UI:
Car_Inspectorpublic override VisualElement CreateInspectorGUI(){ // Create a new VisualElement to be the root of our Inspector UI. VisualElement myInspector = new VisualElement(); // Add a simple label. myInspector.Add(new Label("This is a custom Inspector")); // Return the finished Inspector UI. return myInspector;} -
Select the GameObject that has thecomponent attached to it. The Inspector now displays the label
Carinstead of the default Inspector.This is a custom Inspector
Custom Inspector with a label
Create a custom Inspector UI with UI Builder
You can create a custom Inspector UI with UI Builder, and use C# script to load and instantiate the UI from the UXML file.
-
To open the UI Builder, select Window > UI Toolkit > UI Builder.
-
Select File > New to create a new Visual Tree Asset.

Custom Inspector with a label
-
To enable Editor-only controls in UI Builder, select thein the Hierarchy view and then select the Editor Extension Authoring checkbox.
<unsaved file>*.uxml
Custom Inspector with a label
-
Drag a label control from the Library to the Hierarchy. This adds a label control to the visual tree.

Custom Inspector with a label
-
In the label control's Inspector panel, update the label text.

Custom Inspector with a label
-
Select File > Save and save the visual tree asto the
Car_Inspector_UXML.uxmlfolder.Assets/create-a-custom-inspector
Use UXML inside a custom Inspector
To use the UXML file you created inside your custom Inspector, assign the file to the custom Inspector, and load and clone it inside the function and add it to the visual tree. To do this, you can use the CloneTree method. You can pass any as a parameter to act as a parent for the created elements.
CreateInspectorGUI()VisualElement-
In, create a public variable for a
Car_Inspector.csin your script and assignVisualTreeAssetto it.Car_Inspector_UXML.uxmlpublic VisualTreeAsset inspectorUXML; -
Update themethod to load the UXML file and clone it into the visual tree. The finished
CreateInspectorGUI()file looks like the following:Car_Inspector.csusing UnityEditor;using UnityEditor.UIElements;using UnityEngine.UIElements;[CustomEditor(typeof(Car))]public class Car_Inspector : Editor{ public VisualTreeAsset inspectorUXML; public override VisualElement CreateInspectorGUI() { // Create a new VisualElement to be the root of our Inspector UI. VisualElement myInspector = new VisualElement(); // Add a simple label. myInspector.Add(new Label("This is a custom Inspector")); // Load the UXML file and clone its tree into the inspector. if (inspectorUXML != null) { VisualElement uxmlContent = inspectorUXML.CloneTree(); myInspector.Add(uxmlContent); } // Return the finished Inspector UI. return myInspector; }} -
In thewindow ofInspector
?
, assign theCar_Inspector.csfile to the Inspector UXML field.Car_Inspector_UXML.uxml -
Select the GameObject that has thecomponent attached to it. The Inspector for the
Carcomponent now displays two labels: one through script, and one through UI Builder/UXML.Car
Custom Inspector with two labels label
Bind a text field
This custom Inspector displays all properties of the class. When you update the UI controls, the values inside the instance of the class change. To do so, add UI controls to the visual tree and bind them to the individual properties of the class.
CarCarTo bind a control to a serialized property, assign the property to the field of the control. When you create a custom inspector, binding is automatic. does an implicit bind after you return your visual tree. For more information, refer to SerializedObject data binding.
binding-pathCreateInspectorGUI()-
Double-clickto open it in UI Builder.
Car_Inspector_UXML.uxml -
Add a TextField control.

Add a text field to the UI
-
In the Inspector panel of the TextField, set the label text to.
Make of the car -
Set the binding path to.
make
Bind a property to a control in UI Builder
-
Add a style classto the Style Class List so that the text field aligns with other fields in the Inspector window. For more information, refer to BaseField.
unity-base-field__aligned
Add a style class to the text field
-
In UI Builder, select File > Save.
-
Select the GameObject that has thecomponent attached to it. The Inspector for the
Carcomponent now displays theCartext field. The text field is bound to theMake of the carproperty of themakeclass.Car
Custom Inspector showing a text field
Bind a property field
To display the properties of the class, add a control for each field. The control must match the property type. For example, you must bind an to an Integer field or an Integer Slider.
CarintInstead of adding a specific control based on the property type, you can use the generic PropertyField control. This control works for most types of serialized properties and generates the default Inspector UI for this property type.
The advantage of a is the Inspector UI automatically adjusts when you change the variable type inside your script. However, you can't get a preview of the control inside the UI Builder because the control type is unknown until the visual tree is bound to a serialized object.
PropertyField-
Double-clickto open it in UI Builder.
Car_Inspector_UXML.uxml -
Add a PropertyField control for theproperties of the
yearBuiltclass, and set the binding path and the label text.Car
Add a property field in UI Builder
-
Add a style classto the Style Class List.
unity-base-field__aligned -
Add a PropertyField control for theproperties of the
colorclass, and set the binding path and the label text.Car -
Add a style classto the Style Class List.
unity-base-field__aligned -
In UI Builder, select File > Save.
-
Select the GameObject that has thecomponent attached to it. The Inspector for the
Carcomponent now displays theCarandYear Builtproperty fields.Paint Color
Custom Inspector with property fields
Create a custom property drawer
Unlike custom Inspectors, property drawers work for any [Serializable] class, not just MonoBehaviour/ScriptableObject types.
A custom property drawer is a custom Inspector UI for a custom serializable class. If that serializable class is part of another serialized object, the custom UI displays that property in the Inspector. In UI Toolkit, the control displays the custom property drawer for a field if one exists.
PropertyField-
In thefolder, create a new script named
create-a-custom-inspectorwith the following content:Tire.csusing System.Collections;using System.Collections.Generic;using UnityEngine;[System.Serializable]public class Tire{ public float airPressure = 21.5f; public int profileDepth = 4;} -
In, add a list
Car.csto theTireclass. The finishedCarfile looks like the following:Car.csusing UnityEngine;public class Car : MonoBehaviour{ public string make = "Toyota"; public int yearBuilt = 1980; public Color color = Color.black; // This car has four tires. public Tire[] tires = new Tire[4];} -
The PropertyField control works with all standard property types and also supports custom serializable classes and arrays. To display the properties of the car's tires, add anotherin
PropertyFieldand bind it toCar_Inspector_UXML.uxml. The finishedtiresfile looks like the following:Car_Inspector_UXML.uxml<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="True"> <ui:TextField picking-mode="Ignore" label="Make of the car" value="filler text" binding-path="make" class="unity-base-field__aligned" /> <uie:PropertyField binding-path="yearBuilt" label="Year Built" class="unity-base-field__aligned" /> <uie:PropertyField binding-path="color" label="Paint Color" class="unity-base-field__aligned" /> <uie:PropertyField label="Tires" binding-path="tires" class="unity-base-field__aligned" /></ui:UXML> -
Select the GameObject with thecomponent attached to it. The Inspector for the
Carcomponent now displays theCarproperty field.Tires
Use a PropertyField control to display an array
Create UI for the custom property drawer
You can create a custom property drawer to customize the look of the individual elements in the list. Instead of deriving from the base class, custom property drawers derive from the class.
TireEditorPropertyDrawerYou can use C# script or UXML to create the UI for the property. This example uses C# script to create the custom UI. To create UI for the custom property, override the CreatePropertyGUI method.
-
In thefolder, Create a new script named
Editorwith the following content:Tire_PropertyDrawer.csusing UnityEditor;using UnityEditor.UIElements;using UnityEngine.UIElements;[CustomPropertyDrawer(typeof(Tire))]public class Tire_PropertyDrawer : PropertyDrawer{ public override VisualElement CreatePropertyGUI(SerializedProperty property) { // Create drawer UI using C#. var popup = new UnityEngine.UIElements.PopupWindow(); popup.text = "Tire Details"; popup.Add(new PropertyField(property.FindPropertyRelative("airPressure"), "Air Pressure (psi)")); popup.Add(new PropertyField(property.FindPropertyRelative("profileDepth"), "Profile Depth (mm)")); // Return the finished UI. return popup; }} -
Select the GameObject that has thecomponent attached to it. The Inspector for the
Carcomponent now displays theCarproperty field with a custom property drawer.Tires
Inspector using a custom property drawer