This guide is for developers experienced with Immediate Mode GUI (IMGUI) to migrate to UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary Toolkit. This guide focuses on the Editor UI, but its information can also apply to the runtime UI as well.
IMGUI is code-driven by calls to the OnGUI
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# scriptsA piece of code that allows you to create your own Components, trigger game events, modify Component properties over time and respond to user input in any way you like. More info
See in Glossary. 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.
With IMGUI, you describe the UI tree when the UI is repainted within the OnGUI()
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 elementsA node of a visual tree that instantiates or derives from the C# VisualElement
class. You can style the look, define the behaviour, and display it on screen as part of the UI. More info
See in Glossary with UI Toolkit in a tree structure called Visual TreeAn object graph, made of lightweight nodes, that holds all the elements in a window or panel. It defines every UI you build with the UI Toolkit.
See in Glossary. Information in the Visual Trees is retained persistently.
IMGUI is based on the OnGUI
()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
OnGUI()` might contain many conditions and different states.
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.
Use the IMGUIContainer
to place IMGUI code inside of a VisualElement
. Everything you can do inside of OnGUI()
is supported.
You can arrange multiple IMGUIContainer
s and lay them out by mixing GUILayout
and UI Toolkit layouts. Note that it’s not possible to add VisualElement
instances inside of an IMGUIContainer
.
The following table lists the equivalent functions between IMGUI and UI Toolkit:
Action | IMGUI | UI Toolkit |
---|---|---|
Create an Editor Window | EditorWindow.OnGUI() |
EditorWindow.CreateGUI() |
Create a Property DrawerA Unity feature that allows you to customize the look of certain controls in the Inspector window by using attributes on your scripts, or by controlling how a specific Serializable class should look More info See in Glossary or a Property Attribute |
PropertyDrawer.OnGUI() |
PropertyDrawer.CreatePropertyGUI() |
Create a custom Editor for the InspectorA Unity window that displays information about the currently selected GameObject, asset or project settings, allowing you to inspect and edit the values. More info See in Glossary |
Editor.OnInspectorGUI() |
Editor.CreateInspectorGUI() |
The following table lists the equivalent methods, classes, and attributes between IMGUI and UI Toolkit:
IMGUI | IMGUI namespaces | UI Toolkit |
---|---|---|
AddCursorRect() |
EditorGUIUtility | Set VisualElement.style.cursor , or set a visual element’s cursor texture in the UI Builder or USS. For more detailed interactivity, use C# events. |
AreaScope |
GUILayout | Scopes are generally not needed in UI Toolkit. See BeginArea() . |
BeginArea() |
GUILayout | To define the area itself, create a visual element and set style.position to Position.Absolute . To create children for the area, create child visual elements under it. |
BeginBuildTargetSelectionGrouping() |
EditorGUILayout | No equivalent. |
BeginChangeCheck() |
EditorGUI | Register callbacks on each element in the change check range. If using a PropertyField as a stand-in for a serialized field in a custom Inspector, use PropertyField.RegisterCallback<SerializedPropertyChangeEvent>() or PropertyField.RegisterValueChangeCallback() . In all other cases, use VisualElement.RegisterCallback<ChangeEvent<T>>() or VisualElement.RegisterValueChangedCallback<T>() . |
BeginDisabledGroup() |
EditorGUI | VisualElement.SetEnabled(false) |
BeginFoldoutHeaderGroup() |
EditorGUI, EditorGUILayout | See Foldout() . |
BeginGroup() |
GUI | See BeginArea() . |
BeginHorizontal() |
EditorGUILayout, GUILayout | See BeginArea() . |
BeginProperty() |
EditorGUI | If you use BeginProperty() /EndProperty() to bind a simple control to a serialized property, you can do that in UI Toolkit by calling BindProperty() , by setting bindingPath , or by setting the binding-path UXML attribute. If you use BeginProperty() /EndProperty() to make a single property out of complex custom UI, that is not supported well in UI Toolkit. |
BeginScrollView() |
EditorGUILayout, GUI, GUILayout | UnityEngine.UIElements.ScrollView |
BeginToggleGroup() |
EditorGUILayout | No equivalent. |
BeginVertical() |
EditorGUILayout, GUILayout | See BeginArea() . |
BoundsField() |
EditorGUI, EditorGUILayout | BoundsField |
BoundsIntField() |
EditorGUI, EditorGUILayout | BoundsIntField |
Box() |
GUI, GUILayout | Box |
BringWindowToBack() |
GUI | See Window() . |
BringWindowToFront() |
GUI | See Window() . |
Button() |
GUI, GUILayout | Button |
CanCacheInspectorGUI() |
EditorGUI | Not needed in retained mode. |
ChangeCheckScope |
EditorGUI | Scopes are generally not needed in UI Toolkit. See BeginChangeCheck() . |
ColorField() |
EditorGUI, EditorGUILayout | ColorField |
CommandEvent() |
EditorGUIUtility | Generally not needed in retained mode. Use C# callbacks to handle events. |
CurveField() |
EditorGUI, EditorGUILayout | CurveField |
DelayedDoubleField() |
EditorGUI, EditorGUILayout |
DoubleField with isDelayed set to true. |
DelayedFloatField() |
EditorGUI, EditorGUILayout |
FloatField with isDelayed set to true. |
DelayedIntField() |
EditorGUI, EditorGUILayout |
IntegerField with isDelayed set to true. |
DelayedTextField() |
EditorGUI, EditorGUILayout |
TextField with isDelayed set to true. |
DisabledScope |
EditorGUI | Scopes are generally not needed in UI Toolkit. See BeginDisabledGroup() . |
DoubleField() |
EditorGUI, EditorGUILayout | DoubleField |
DragWindow() |
GUI | See Window() . |
DrawPreviewTexture() |
EditorGUI | No equivalent. |
DrawRect() |
EditorGUI | Use VisualElement . Set style.position to Absolute . Set style.top and style.left to define the position. Set style.width and style.height to define the size. Set style.backgroundColor to set the color. |
DrawTexture() |
GUI |
Image . Set tintColor in place of color . There is no equivalent for a false alphaBlend . There are no equivalents for borderWidth , borderWidths , borderRadius , or borderRadiuses . |
DrawTextureAlpha() |
EditorGUI | No equivalent. |
DrawTextureWithTexCoords() |
GUI |
Image . Set uv in place of texCoords . There is no equivalent for a false alphaBlend . |
DropdownButton() |
EditorGUI, EditorGUILayout | No exact equivalent. Use fully-fledged DropdownField s instead of just a DropdownButton() . |
DropShadowLabel() |
EditorGUI |
Label with shadow values set in style.textShadow . |
EditorToolbar() |
EditorGUILayout | Create a Toolbar with one ToolbarButton for each tool. For each ToolbarButton , register a callback when clicked to call either ToolManager.SetActiveTool() or ToolManager.RestorePreviousTool() to make that button activate the tool or deactivate it, respectively. |
EndArea() |
GUILayout | See BeginArea() . |
EndBuildTargetSelectionGrouping() |
EditorGUILayout | See BeginBuildTargetSelectionGrouping() . |
EndChangeCheck() |
EditorGUI | See BeginChangeCheck() . |
EndDisabledGroup() |
EditorGUI | See BeginDisabledGroup() . |
EndFoldoutHeaderGroup() |
EditorGUI, EditorGUILayout | See Foldout() . |
EndGroup() |
GUI | See BeginArea() . |
EndHorizontal() |
EditorGUILayout, GUILayout | See BeginArea() . |
EndProperty() |
EditorGUI | See BeginProperty() . |
EndScrollView() |
EditorGUILayout, GUI, GUILayout | See BeginScrollView() . |
EndToggleGroup() |
EditorGUILayout | See BeginToggleGroup() . |
EndVertical() |
EditorGUILayout, GUILayout | See BeginArea() . |
EnumFlagsField() |
EditorGUI, EditorGUILayout | EnumFlagsField |
EnumPopup() |
EditorGUI, EditorGUILayout | EnumField |
ExpandHeight() |
GUILayout | No equivalent. |
ExpandWidth() |
GUILayout | No equivalent. |
FlexibleSpace() |
GUILayout | See Space() . |
FloatField() |
EditorGUI, EditorGUILayout | FloatField |
FocusControl() |
GUI | VisualElement.Focus() |
FocusTextInControl() |
EditorGUI | TextField.Focus() |
FocusWindow() |
GUI | See Window() . |
Foldout() |
EditorGUI, EditorGUILayout | Foldout |
GetControlRect() |
EditorGUILayout | Only needed to convert from EditorGUILayout to EditorGUI. Not needed in UI Toolkit. |
GetNameOfFocusedControl() |
GUI | VisualElement.focusController.focusedElement |
GetPropertyHeight() |
EditorGUI | PropertyField.layout.height |
GradientField() |
EditorGUI, EditorGUILayout | GradientField |
GroupScope |
GUI | Scopes are generally not needed in UI Toolkit. See BeginArea() . |
Height() |
GUILayout | VisualElement.style.height |
HelpBox() |
EditorGUI, EditorGUILayout | HelpBox |
HorizontalScope |
EditorGUILayout, GUILayout | Scopes are generally not needed in UI Toolkit. See BeginArea() . |
HorizontalScrollbar() |
GUI, GUILayout |
Scroller with direction set to Horizontal . |
HorizontalSlider() |
GUI, GUILayout |
Slider with direction set to Horizontal
|
InspectorTitlebar() |
EditorGUI, EditorGUILayout | No equivalent. |
IntField() |
EditorGUI, EditorGUILayout | IntegerField |
IntPopup() |
EditorGUI, EditorGUILayout | No equivalent. |
IntSlider() |
EditorGUI, EditorGUILayout | SliderInt |
Label() |
GUI, GUILayout | Label |
LabelField() |
EditorGUI, EditorGUILayout |
TextField with isReadOnly set to true. |
LayerField() |
EditorGUI, EditorGUILayout | LayerField |
LinkButton() |
EditorGUI, EditorGUILayout | No equivalent. |
Load() |
EditorGUIUtility | If using C#, you can use this function as is and assign its return value to the VisualElement.style property you want. If using USS, use function resource() with the same argument you would give to Load() . |
LongField() |
EditorGUI, EditorGUILayout | LongField |
MaskField() |
EditorGUI, EditorGUILayout | MaskField |
MaxHeight() |
GUILayout | VisualElement.style.maxHeight |
MaxWidth() |
GUILayout | VisualElement.style.maxWidth |
MinHeight() |
GUILayout | VisualElement.style.minHeight |
MinMaxSlider() |
EditorGUI, EditorGUILayout | MinMaxSlider |
MinWidth() |
GUILayout | VisualElement.style.minWidth |
ModalWindow() |
GUI | See Window() . |
[NonReorderable] attribute |
Ensure that ListView.reorderable is false. |
|
ObjectField() |
EditorGUI, EditorGUILayout | ObjectField |
PasswordField() |
EditorGUI, EditorGUILayout, GUI, GUILayout |
TextField with isPasswordField set to true |
PixelsToPoints() |
EditorGUIUtility | Valid for use with UI Toolkit. |
PointsToPixels() |
EditorGUIUtility | Valid for use with UI Toolkit. |
Popup() |
EditorGUI, EditorGUILayout | PopupField<T0> |
ProgressBar() |
EditorGUI | ProgressBar |
PropertyField() |
EditorGUI, EditorGUILayout | PropertyField |
PropertyScope |
EditorGUI | Scopes are generally not needed in UI Toolkit. See BeginProperty() . |
RectField() |
EditorGUI, EditorGUILayout | RectField |
RectIntField() |
EditorGUI, EditorGUILayout | RectIntField |
RepeatButton() |
GUI, GUILayout | RepeatButton |
ScrollTo() |
GUI |
ScrollView.ScrollTo() or ScrollView.scrollOffset
|
ScrollViewScope |
EditorGUILayout, GUI, GUILayout | Scopes are generally not needed in UI Toolkit. See BeginScrollView() . |
SelectableLabel() |
EditorGUI, EditorGUILayout |
Label with isSelectable and focusable set to true. |
SelectionGrid() |
GUI, GUILayout | RadioButton |
SetNextControlName() |
GUI | VisualElement.name |
singleLineHeight |
EditorGUIUtility | Use USS variable --unity-metrics-single_line-height . |
Slider() |
EditorGUI, EditorGUILayout | Slider |
Space() |
EditorGUILayout, GUILayout | Use flex properties to configure spacing between visual elements. |
TagField() |
EditorGUI, EditorGUILayout | TagField |
TextArea() |
EditorGUI, EditorGUILayout, GUI, GUILayout |
TextField with multiline set to true, style.whiteSpace set to Normal , and ScrollView.verticalScrollerVisibility set to Auto . |
TextField() |
EditorGUI, EditorGUILayout, GUI, GUILayout |
TextField with multiline set to true and style.whiteSpace set to NoWrap . |
Toggle() |
EditorGUI, EditorGUILayout, GUI, GUILayout | Toggle |
ToggleGroupScope |
EditorGUILayout | Scopes are generally not needed in UI Toolkit. See BeginToggleGroup() . |
ToggleLeft() |
EditorGUI, EditorGUILayout |
Toggle , but instead of setting label , set text . |
Toolbar() |
GUI, GUILayout | No equivalent. |
UnfocusWindow() |
GUI | See Window() . |
Vector2Field() |
EditorGUI, EditorGUILayout | Vector2Field |
Vector2IntField() |
EditorGUI, EditorGUILayout | Vector2IntField |
Vector3Field() |
EditorGUI, EditorGUILayout | Vector3Field |
Vector3IntField() |
EditorGUI, EditorGUILayout | Vector3IntField |
Vector4Field() |
EditorGUI, EditorGUILayout | Vector4Field |
VerticalScope |
EditorGUILayout, GUILayout | Scopes are generally not needed in UI Toolkit. See BeginArea() . |
VerticalScrollbar() |
GUI, GUILayout |
Scroller with direction set to Vertical . |
VerticalSlider() |
GUI, GUILayout |
Slider with direction set to Vertical . |
Width() |
GUILayout | VisualElement.style.width |
Window() |
GUI, GUILayout | No equivalent. |
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.