Introduction to UXML
Understand the format of the UXML.
읽는 시간 4분최근 업데이트: 한 달 전
The UXML format is inspired by HTML, XAML, and XML. If you've worked with these formats before, you'll find similarities in UXML. However, the UXML format includes small differences to provide an efficient way to work with Unity. This page introduces the format of UXML by an example.
The following is an example UXML file. It contains several built-in UI controls that prompt the user to make a choice:
<?xml version="1.0" encoding="utf-8"?><engine:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:engine="UnityEngine.UIElements" xmlns:editor="UnityEditor.UIElements" xsi:noNamespaceSchemaLocation="../../UIElementsSchema/UIElements.xsd"> <engine:Box> <engine:Toggle name="boots" label="Boots" value="false" /> <engine:Toggle name="helmet" label="Helmet" value="false" /> <engine:Toggle name="cloak" label="Cloak of invisibility" value="false"/> </engine:Box> <engine:Box> <engine:Button name="cancel" text="Cancel" /> <engine:Button name="ok" text="OK" /> </engine:Box></engine:UXML>
UXML declaration
The first line of code in the above UXML example is the UXML declaration. The declaration is optional. If you do include the UXML declaration, you must put it on the first line. A UXML declaration requires a . The attribute is optional in a UXML declaration. If you include the attribute, you must declare the character encoding of the file.
versionencodingencodingDocument root
The next line defines the document root, . The element includes attributes for the namespace prefix definitions and the location of schema definition files. You can specify these attributes in any order.
<engine:UXML><engine:UXML>Namespace
In UI Toolkit, each element is defined in either the or the namespace:
UnityEngine.UIElementsUnityEditor.UIElements- The namespace contains elements that are defined as part of the Unity runtime.
UnityEngine.UIElements - The namespace contains elements that are available in the Unity Editor. To fully specify an element, you must prefix it with its namespace.
UnityEditor.UIElements
For example, if you want to use the element in your UXML template, you must specify .
Button<UnityEngine.UIElements:Button />To make specifying namespaces easier, you can define a namespace prefix. For example, defines the prefix as . Once you define a namespace prefix, you can use it to specify namespaces. For example, is equivalent to
xmlns:engine="UnityEngine.UIElements"engineUnityEngine.UIElements<engine:Button /><UnityEngine.UIElements:Button />You can also define a default namespace by excluding a prefix. For example, defines as the default namespace. This means that specifying, for example, is equivalent to
xmlns="UnityEngine.UIElements"UnityEngine.UIElements<Button /><UnityEngine.UIElements:Button />To create a UXML file with automatically defined namespaces, select Asset > Create > UI Toolkit > UI Document.
Schema definition
The schema definition validates your UXML files. It specifies which attributes and child elements each UXML element can contain.
In a UXML file, the or the attributes of the root element specify the location of the schema definition files.
xsi:noNamespaceSchemaLocationxsi:schemaLocation<engine:UXML>To create a UXML file with the latest schema definition, select Assets > Create > UI Toolkit > UI Document. This creates a file with the attribute for the root element.
xsi:noNamespaceSchemaLocationTo force an update of the UXML schema definition, select Assets > Update UXML Schema.
UXML elements
The definition of the UI is within the root. The UI definition is a series of nested UXML elements, each representing a visual element.
<engine:UXML>The element name corresponds to the C# class name of the element to instantiate. Most elements have attributes and their values are mapped to the corresponding class properties in C#. Each element has its own set of attributes in addition to the inherited attributes of its parent class. is the base class for all elements and it provides the following attributes for all elements:
VisualElement- : An identifier for the element. The name should be unique.
name - : Set to either
picking-modeto respond to mouse events orPositionto ignore mouse events.Ignore - : (OBSOLETE) Use
focus-indexandtabIndex.focusable - : An integer that defines the tabbing position of the current element.
tabindex - : A Boolean that indicates whether the element is focusable.
focusable - : A space-separated list of identifiers that characterize the element. Use classes to assign visual styles to elements. You can also use classes to select a set of elements in UQuery.
class - : A string that displays as a tooltip when the mouse hovers over the element.
tooltip - : A string that defines the key used for serialization of the element.
view-data-key