Migrate from Immediate Mode GUI (IMGUI) to UI Toolkit
Understand the similarities and differences between IMGUI and the UI Toolkit.
Read time 8 minutesLast updated 24 days ago
This guide is for developers experienced with Immediate Mode GUI (IMGUI) to migrate to UI Toolkit. This guide focuses on the Editor UI, but its information can also apply to the runtime UI as well.
Key differences
Code-driven versus UI-driven
IMGUI is code-driven by calls to the function in a C# script that implements it. UI Toolkit provides more options for Editor UI creation. With UI Toolkit, you define the behaviors in C# scripts. However, when defining UI elements and styles, in addition to C#, you can visually define UI controls in UI Builder or write in an XML-like text file (called UXML) directly. For more information, refer to Get started with UI Toolkit.
OnGUIImmediate versus retained mode
With IMGUI, you describe the UI tree when the UI is repainted within the function. You must call this function when an event enters the UI or when you repaint the UI. There is no persistent information pertaining to the UI tree between different events. Whereas, you create visual elements with UI Toolkit in a tree structure called Visual Tree. Information in the Visual Trees is retained persistently.
OnGUI()Constant versus state changes
IMGUI is based on the function that runs at least once every frame. You define the look and the behaviors of the UI for every possible frame. The body of might contain many conditions and different states.
OnGUI()OnGUI()UI Toolkit operates in an event-driven system. You define the look of the UI in its default state and define the behaviors of the UI in response to events. Any changes you make in UI Toolkit cause persistent changes to the state of your UI.
For example, the declaration of a button in IMGUI looks like the following:
if (GUILayout.Button("Click me!")){ //Code runs here in frames where the user clicks the button. //Code makes changes to the UI for frames where the user has just clicked the button.}else{ //Code specifies what happens in all other frames.}
The example above looks like the following in UI Toolkit:
UIDocument document = GetComponent<UIDocument>();//Create button.Button button = new Button();button.text = "Click me!";//Set up event handler.button.RegisterCallback<ClickEvent>((ClickEvent evt) =>{ //Code runs here after button receives ClickEvent.});//Add button to UI.document.rootVisualElement.Add(button);
For a completed example of how to create a custom Editor window with UI Toolkit, refer to Get started with UI Toolkit.
IMGUI support
Use the to place IMGUI code inside of a . Everything you can do inside of is supported.
IMGUIContainerVisualElementOnGUI()You can arrange multiple s and lay them out by mixing and UI Toolkit layouts. Note that it's not possible to add instances inside of an .
IMGUIContainerGUILayoutVisualElementIMGUIContainer
IMGUIContainer example
From IMGUI to UI Toolkit conversion
The following table lists the equivalent functions between IMGUI and UI Toolkit:
Action | IMGUI | UI Toolkit |
|---|---|---|
| Create an Editor Window | | |
| Create a Property Drawer or a Property Attribute | | |
| Create a custom Editor for the Inspector | | |
The following table lists the equivalent methods, classes, and attributes between IMGUI and UI Toolkit:
IMGUI | IMGUI namespaces | UI Toolkit |
|---|---|---|
| EditorGUIUtility | Set |
| GUILayout | Scopes are generally not needed in UI Toolkit. See |
| GUILayout | To define the area itself, create a visual element and set |
| EditorGUILayout | No equivalent. |
| EditorGUI | Register callbacks on each element in the change check range. If using a |
| EditorGUI | |
| EditorGUI, EditorGUILayout | See |
| GUI | See |
| EditorGUILayout, GUILayout | See |
| EditorGUI | If you use |
| EditorGUILayout, GUI, GUILayout | |
| EditorGUILayout | No equivalent. |
| EditorGUILayout, GUILayout | See |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | |
| GUI, GUILayout | |
| GUI | See |
| GUI | See |
| GUI, GUILayout | |
| EditorGUI | Not needed in retained mode. |
| EditorGUI | Scopes are generally not needed in UI Toolkit. See |
| EditorGUI, EditorGUILayout | |
| EditorGUIUtility | Generally not needed in retained mode. Use C# callbacks to handle events. |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | |
| EditorGUI | Scopes are generally not needed in UI Toolkit. See |
| EditorGUI, EditorGUILayout | |
| GUI | See |
| EditorGUI | No equivalent. |
| EditorGUI | Use |
| GUI | |
| EditorGUI | No equivalent. |
| GUI | |
| EditorGUI, EditorGUILayout | No exact equivalent. Use fully-fledged |
| EditorGUI | |
| EditorGUILayout | Create a |
| GUILayout | See |
| EditorGUILayout | See |
| EditorGUI | See |
| EditorGUI | See |
| EditorGUI, EditorGUILayout | See |
| GUI | See |
| EditorGUILayout, GUILayout | See |
| EditorGUI | See |
| EditorGUILayout, GUI, GUILayout | See |
| EditorGUILayout | See |
| EditorGUILayout, GUILayout | See |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | |
| GUILayout | No equivalent. |
| GUILayout | No equivalent. |
| GUILayout | See |
| EditorGUI, EditorGUILayout | |
| GUI | |
| EditorGUI | |
| GUI | See |
| EditorGUI, EditorGUILayout | |
| EditorGUILayout | Only needed to convert from EditorGUILayout to EditorGUI. Not needed in UI Toolkit. |
| GUI | |
| EditorGUI | |
| EditorGUI, EditorGUILayout | |
| GUI | Scopes are generally not needed in UI Toolkit. See |
| GUILayout | |
| EditorGUI, EditorGUILayout | |
| EditorGUILayout, GUILayout | Scopes are generally not needed in UI Toolkit. See |
| GUI, GUILayout | |
| GUI, GUILayout | |
| EditorGUI, EditorGUILayout | No equivalent. |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | No equivalent. |
| EditorGUI, EditorGUILayout | |
| GUI, GUILayout | |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | No equivalent. |
| EditorGUIUtility | If using C#, you can use this function as is and assign its return value to the |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | |
| GUILayout | |
| GUILayout | |
| GUILayout | |
| EditorGUI, EditorGUILayout | |
| GUILayout | |
| GUI | See |
| Ensure that | |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout, GUI, GUILayout | |
| EditorGUIUtility | |
| EditorGUIUtility | use |
| EditorGUIUtility | use |
| EditorGUI, EditorGUILayout | |
| EditorGUI | |
| EditorGUI, EditorGUILayout | |
| EditorGUI | Scopes are generally not needed in UI Toolkit. See |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | |
| GUI, GUILayout | |
| GUI | |
| EditorGUILayout, GUI, GUILayout | Scopes are generally not needed in UI Toolkit. See |
| EditorGUI, EditorGUILayout | |
| GUI, GUILayout | |
| GUI | |
| EditorGUIUtility | Use USS variable |
| EditorGUI, EditorGUILayout | |
| EditorGUILayout, GUILayout | Use |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout, GUI, GUILayout | |
| EditorGUI, EditorGUILayout, GUI, GUILayout | |
| EditorGUI, EditorGUILayout, GUI, GUILayout | |
| EditorGUILayout | Scopes are generally not needed in UI Toolkit. See |
| EditorGUI, EditorGUILayout | |
| GUI, GUILayout | No equivalent. |
| GUI | See |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | |
| EditorGUI, EditorGUILayout | |
| EditorGUILayout, GUILayout | Scopes are generally not needed in UI Toolkit. See |
| GUI, GUILayout | |
| GUI, GUILayout | |
| GUILayout | |
| GUI, GUILayout | No equivalent. |