Get started with UI Toolkit
Follow a simple workflow to get started with UI Toolkit.
Read time 7 minutesLast updated a month ago
Want to create your first UI with UI Toolkit? Use this basic UI Toolkit workflow example to get started.
If you perform a specific task often, you can use UI Toolkit to create a dedicated UI for it. For example, you can create a custom Editor window. The example demonstrates how to create a custom Editor window and add UI controls into your custom Editor window with UI Builder, UXML, and C# script.
You can find the completed files that this example creates in this GitHub repository.
Create a custom Editor window
Create a custom Editor window with two labels.
- Create a project in Unity Editor with any template.
- In the Project window, right-click in the folder, and then select Create > UI Toolkit > Editor Window.
Assets - In UI Toolkit Editor Window Creator, enter in the C# box.
SimpleCustomEditor - Keep the UXML checkbox selected and clear the USS checkbox.
- Select Confirm.
- To open the Editor window, select Window > UI Toolkit > SimpleCustomEditor.
You can find the source files for it in the folder.
Assets/EditorAdd UI controls to the window
You can add UI controls to your window in the following ways:
- Use the UI Builder to visually add the UI controls
- Use an XML-like text file (UXML) to add the UI controls
- Use C# script to add the UI controls
You can use any of these methods individually, or combine. The following examples create three sets of labels, buttons, and toggles with a combination of these methods.
Use UI Builder to add UI controls
To visually add UI controls to your window, use UI Builder. The following steps add a button and a toggle into your custom Editor window in addition to the default label.
- In the folder, double-click
Editorto open the UI Builder.SimpleCustomEditor.uxml - In the UI Builder, drag Button and Toggle from Library > Controls into the Hierarchy or the window preview in the .Viewport
?
- In the Hierarchy window, select Label.
- In the window, change the default text toInspector
?
in the Text field.These controls were created in UI Builder - In the Hierarchy window, select Button.
- In the Inspector window, enter in the Text field.
This is button1 - Enter in the Name field.
button1 - In the Hierarchy window, select Toggle.
- In the Inspector window, enter in the Label field.
Number? - Enter in the Name field.
toggle1 - Save and close the UI Builder window.
- Close your custom Editor window if you haven't done so.
- In the Project window, select and make sure the Visual Tree Asset is set to
SimpleCustomEditor.csin the Inspector window.SimpleCustomEditor (Visual Tree Asset) - Select Window > UI Toolkit > SimpleCustomEditor to re-open your custom Editor window to see the button and the toggle you just added.

Custom Editor Window with one set UI Controls
Use UXML to add UI controls
If you prefer to define your UI in a text file, you can edit the UXML to add the UI controls. The following steps add another set of label, button, and toggle into your window.
-
In thefolder, select Assets > Create > UI Toolkit > UI Document to create a UXML file called
Editor.SimpleCustomEditor_UXML.uxml -
Select the arrow onin the Project window.
SimpleCustomEditor_UXML.uxml -
Double-clickto open
inlineStylein a text editor.SimpleCustomEditor_UXML.uxml -
Replace the contents ofwith the following:
SimpleCustomEditor_uxml.uxml<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False"> <ui:Label text="These controls were created with UXML." /> <ui:Button text="This is button2" name="button2"/> <ui:Toggle label="Number?" name="toggle2"/></ui:UXML> -
Open.
SimpleCustomEditor.cs -
Import the UXML file you created manually by adding the following to themethod:
CreateGUI// Import UXML created manually.var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/SimpleCustomEditor_uxml.uxml");VisualElement labelFromUXML = visualTree.Instantiate();root.Add(labelFromUXML); -
Save your changes.
-
Select Window > UI Toolkit > SimpleCustomEditor. This opens your custom Editor window with three labels, two buttons, and two toggles.

Custom Editor Window with two sets UI Controls
Use C# script to add UI controls
If you prefer coding, you can add UI Controls to your window with a C# script. The following steps add another set of label, button, and toggle into your window.
-
Open.
SimpleCustomEditor.cs -
Unity usesfor basic UI controls like label, button, and toggle. To work with UI controls, you must add the following declaration if it's not already present.
UnityEngine.UIElementsusing UnityEngine.UIElements; -
Change the text of the existing label fromto
"Hello World! From C#"."These controls were created using C# code." -
The EditorWindow class has a property called. To add the UI controls to your window, first instantiate the element class with some attributes, and then use the
rootVisualElementmethods of theAdd.rootVisualElementYour finishedmethod looks like the following:CreateGUI()public void CreateGUI(){ // Each editor window contains a root VisualElement object VisualElement root = rootVisualElement; // VisualElements objects can contain other VisualElements following a tree hierarchy. Label label = new Label("These controls were created using C# code."); root.Add(label); Button button = new Button(); button.name = "button3"; button.text = "This is button3."; root.Add(button); Toggle toggle = new Toggle(); toggle.name = "toggle3"; toggle.label = "Number?"; root.Add(toggle); // Instantiate UXML created automatically which is set as the default VisualTreeAsset. root.Add(m_VisualTreeAsset.Instantiate()); // Import UXML created manually. var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/SimpleCustomEditor_uxml.uxml"); VisualElement labelFromUXML = visualTree.Instantiate(); root.Add(labelFromUXML);} -
Select Window > UI Toolkit > SimpleCustomEditor to open your custom Editor window to see three labels, three buttons, and three toggles.

Custom Editor Window with three Controls
Define the behavior of your UI controls
You can set up event handlers for your UI controls so that when you click the button, and select or clear the toggle, your UI controls perform some tasks.
In this example, set up event handlers that:
- When a button is clicked, the Editor Console displays a message.
- When a toggle is selected, the Console shows how many times the buttons have been clicked.
Your finished looks like the following:
SimpleCustomEditor.csusing UnityEditor;using UnityEngine;using UnityEngine.UIElements;public class SimpleCustomEditor : EditorWindow{ [SerializeField] private VisualTreeAsset m_VisualTreeAsset = default; [MenuItem("Window/UI Toolkit/SimpleCustomEditor")] public static void ShowExample() { SimpleCustomEditor wnd = GetWindow<SimpleCustomEditor>(); wnd.titleContent = new GUIContent("SimpleCustomEditor"); } private int m_ClickCount = 0; private const string m_ButtonPrefix = "button"; public void CreateGUI() { // Each editor window contains a root VisualElement object VisualElement root = rootVisualElement; // VisualElements objects can contain other VisualElement following a tree hierarchy. Label label = new Label("These controls were created using C# code."); root.Add(label); Button button = new Button(); button.name = "button3"; button.text = "This is button3."; root.Add(button); Toggle toggle = new Toggle(); toggle.name = "toggle3"; toggle.label = "Number?"; root.Add(toggle); // Instantiate UXML created automatically which is set as the default VisualTreeAsset. root.Add(m_VisualTreeAsset.Instantiate()); // Import UXML created manually. var visualTree = AssetDatabase.LoadAssetAtPath<VisualTreeAsset>("Assets/Editor/SimpleCustomEditor_uxml.uxml"); VisualElement labelFromUXML = visualTree.Instantiate(); root.Add(labelFromUXML); //Call the event handler SetupButtonHandler(); } //Functions as the event handlers for your button click and number counts private void SetupButtonHandler() { VisualElement root = rootVisualElement; var buttons = root.Query<Button>(); buttons.ForEach(RegisterHandler); } private void RegisterHandler(Button button) { button.RegisterCallback<ClickEvent>(PrintClickMessage); } private void PrintClickMessage(ClickEvent evt) { VisualElement root = rootVisualElement; ++m_ClickCount; //Because of the names we gave the buttons and toggles, we can use the //button name to find the toggle name. Button button = evt.currentTarget as Button; string buttonNumber = button.name.Substring(m_ButtonPrefix.Length); string toggleName = "toggle" + buttonNumber; Toggle toggle = root.Q<Toggle>(toggleName); Debug.Log("Button was clicked!" + (toggle.value ? " Count: " + m_ClickCount : "")); }}
Test the example
- From the menu, select Window > UI Toolkit > SimpleCustomEditor.