The most basic building block in UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary Toolkit is a visual element. The visual elements are ordered into a hierarchy tree with parent-child relationships. The diagram below displays a simplified example of the hierarchy tree, and the rendered result in UI Toolkit.
The VisualElement class is the base for all nodes in the visual tree. The VisualElement
base class contains common properties for all controls, such as styles, layout data, and event handlers. Visual elements can have children and descendant visual elements. For example, in the diagram above, the first Box
visual element has three child visual elements: Label
, Checkbox
, and Slider
.
You can customize the appearance of visual elements through stylesheets. You can also use event callbacks to modify the behavior of a visual element.
VisualElement
derives into subclasses that define additional behavior and functionality, such as controls. UI Toolkit includes a variety of built-in controls with specialized behavior. For example, the following items are available as built-in controls:
You can also combine visual elements together and modify their behavior to create custom controls. For a list of built-in controls, see the Control reference page.
The panel is the parent object of the visual tree. A visual tree must connect to a panel for the visual elements inside a tree to render. All panels belong to a window, such as an Editor Window. The panel also handles focus control and the event dispatching for the visual tree.
Every element in a visual tree holds a direct reference to the panel that holds the visual tree. To verify the connection of a VisualElement
to a panel, you can test the panel
property of this element. When the visual element isn’t connected, the test returns null
.
The draw order of elements in the visual tree follows a depth-first search. Child visual elements appear on top of parent elements. Child elements are also drawn in the order of the sibling list. The draw order is the following:
The diagram below shows the draw order of the previous example:
To change the drawing order of visual elements, use the following functions:
For sibling visual elements, use the following:
UI Toolkit uses a powerful layout system that automatically calculates the position and size of individual elements based on the layout parameters in their style properties. For more information, see the Layout Engine page.
UI Toolkit has two types of coordinates:
Each visual element determines the coordinate system used to calculate its position. You can configure which coordinate system to use in the element style sheet.
The following code shows how to set the coordinate space and the position of a visual element through code:
var newElement = new VisualElement();
newElement.style.position = Position.Relative;
newElement.style.left = 15;
newElement.style.top = 35;
The origin of an element is its top left corner.
The layout system computes the VisualElement.layout
property (type Rect
) for each element, which includes the final position of the element. This takes the relative or absolute position of the element into account.
The layout.position
is expressed in points, relative to the coordinate space of its parent.
Each VisualElement
has a transform property (ITransform
) you can use to add an additional local offset to the position and rotation of an element. The offset isn’t represented in the calculated layout property. By default, the transform
is the identity.
Use the worldBound
property to retrieve the final window space coordinates of the VisualElement
, taking into account both the layout position and the transform. This position includes the height of the header of the window.
The following code sample demonstrates the difference between relative and absolute positioning. It uses the automatic layout system to add boxes to a window and calculate their positions. One box demonstrates a relative offset of 25 px
, while another box demonstrates the absolute position of 25 px, 25 px
.
To see this example in action, do the following:
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
public class PositioningTestWindow : EditorWindow
{
[MenuItem("Window/UI Toolkit/Positioning Test Window")]
public static void ShowExample()
{
var wnd = GetWindow<PositioningTestWindow>();
wnd.titleContent = new GUIContent("Positioning Test Window");
}
public void CreateGUI()
{
for (int i = 0; i < 2; i++)
{
var temp = new VisualElement();
temp.style.width = 70;
temp.style.height = 70;
temp.style.marginBottom = 2;
temp.style.backgroundColor = Color.gray;
rootVisualElement.Add(temp);
}
// Relative Positioning
var relative = new Label("Relative\nPos\n25, 0");
relative.style.width = 70;
relative.style.height = 70;
relative.style.left = 25;
relative.style.marginBottom = 2;
relative.style.backgroundColor = new Color(0.2165094f, 0, 0.254717f);
rootVisualElement.Add(relative);
for (int i = 0; i < 2; i++)
{
var temp = new VisualElement();
temp.style.width = 70;
temp.style.height = 70;
temp.style.marginBottom = 2;
temp.style.backgroundColor = Color.gray;
rootVisualElement.Add(temp);
}
// Absolute Positioning
var absolutePositionElement = new Label("Absolute\nPos\n25, 25");
absolutePositionElement.style.position = Position.Absolute;
absolutePositionElement.style.top = 25;
absolutePositionElement.style.left = 25;
absolutePositionElement.style.width = 70;
absolutePositionElement.style.height = 70;
absolutePositionElement.style.backgroundColor = Color.black;
rootVisualElement.Add(absolutePositionElement);
}
}
The VisualElement.layout.position
and VisualElement.transform
properties define how to transform between the local coordinate system and the parent coordinate system.
The VisualElementExtensions static class provides the following extension methods that transform points and rectangles between coordinate systems:
WorldToLocal
transforms a Vector2
or Rect
from Panel
space to the referential within the element.LocalToWorld
transforms a Vector2
or Rect
to Panel
space referential.ChangeCoordinatesTo
transforms Vector2
or Rect
from the local space of one element to the local space of another.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.