Version: 2020.2
The UXML format
Loading UXML from C#

Writing UXML Templates

UXML templates are text files written using XML markup that define the logical structure of the user interface. The following code example shows how to define a simple panel that prompts the user to make a choice:

<?xml version="1.0" encoding="utf-8"?>
<UXML
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="UnityEngine.UIElements"
    xsi:noNamespaceSchemaLocation="../UIElementsSchema/UIElements.xsd"
    xsi:schemaLocation="UnityEngine.UIElements ../UIElementsSchema/UnityEngine.UIElements.xsd">

    <Label text="Select something to remove from your suitcase:"/>
    <Box>
        <Toggle name="boots" label="Boots" value="false" />
        <Toggle name="helmet" label="Helmet" value="false" />
        <Toggle name="cloak" label="Cloak of invisibility" value="false"/>
    </Box>
    <Box>
        <Button name="cancel" text="Cancel" />
        <Button name="ok" text="OK" />
    </Box>
</UXML>

The first line of the file is the XML declaration. The declaration is optional. If the declaration is included, it must be on the first line and no other content or white space may appear before it. The version attribute is required. The encoding attribute is optional. If encoding is included, it must declare the character encoding of the file.

The next line defines the document root, <UXML>. The <UXML> element includes attributes for the namespace prefix definitions and the location of schema definition files. You can specify these attributes in no partiular order.

In UI(User Interface) Allows a user to interact with your application. More info
See in Glossary
Toolkit, each element is defined in either the UnityEngine.UIElements or the UnityEditor.UIElements namespace:

  • The UnityEngine.UIElements namespace contains elements that are defined as part of the Unity Runtime.
  • The UnityEditor.UIElements namespace contains elements that are available in the Unity Editor. To fully specify an element, you must prefix it with its namespace.

For example, if you want to use the Button element in your UXML template, you must specify <UnityEngine.UIElements:Button />.

To make specifying namespaces easier, you can define a namespace prefix. For example, xmlns:engine="UnityEngine.UIElements" defines the engine prefix as UnityEngine.UIElements. Once a namespace prefix is defined, you can use it to specify namespaces. For example, <engine:Button /> is equivalent to <UnityEngine.UIElements:Button />

You can also define a default namespace by excluding a prefix. For example, the line xmlns="UnityEngine.UIElements" defines UnityEngine.UIElements as the default namespace. This means that specifying, for example, <Button /> is equivalent to <UnityEngine.UIElements:Button />

If you define your own elements, these elements are probably defined in their own namespace. If you want to use these elements in your UXML template, you must include the namespace definition and schema file location in the <UXML> tag, along with the Unity namespaces.

When you create a new UXML template asset by selecting Asset > Create > UI Toolkit > Editor Window, the Editor automatically defines namespaces for you.

The definition of the UI is within the <UXML> root. The UI defintition is a series of nested XML elements, each representing a VisualElement.

The element name corresponds to the C# class name of the element to instantiate. Most elements have attributes and their values are mapped to corresponding class properties in C#. Each element inherits the attributes of its parent class type, to which it can add its own set of attributes. VisualElement being the base class for all elements, it provides the following attributes for all elements:

  • name: an identifier for the element. The name should be unique.
  • picking-mode: set to either Position to respond to mouse events or Ignore to ignore mouse events.
  • focus-index: (OBSOLETE) Use tabIndex and focusable.
  • tabindex: an integer that defines the tabbing position of the current element.
  • focusable: a boolean indicating whether the element is focusable.
  • class: 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.
  • tooltip: a string that displays as a tooltip when the mouse hovers over the element.
  • view-data-key : a string that defines the key used for serialization of the element.

The UXML template example does not define the visual aspect of the user interface. It is recommended that you define style information, such as the dimensions, fonts, and colors for drawing the UI, in a separate USS file (see Styles and Unity style sheets).

Adding styles to UXML

To reference a stylesheet file, A UXML file can use the <Style> element under any element declaration.

For example, if you have a UXML file and a USS file named “styles.uss” in the same folder:

<engine:UXML ...>
    <engine:VisualElement class="root">
        <Style src="styles.uss" />
    </engine:VisualElement>
</engine:UXML>
#root {
    width: 200px;
    height: 200px;
    background-color: red;
}

Note: Unity does not support <Style> elements under the root <UXML> element.

You can also declare in-line styles directly as an attribute of UXML elements:

<engine:UXML ...>
    <engine:VisualElement style="width: 200px; height: 200px; background-color: red;" />
</engine:UXML>

Reusing UXML files

You can create components by simply defining it in a UXML file and import it using the <Template> and <Instance> elements in another UXML file.

When designing a large user interface, you can create template UXML files that define parts of the UI.

You could use the same UI definitions in many places. For example, say that you have a portrait UI element that has an image, name, and a label. You can create a UXML template file to resuse the portrait UI elment in other UXML files.

For example, say that you have a Portrait component in the file Assets/Portrait.uxml:

<engine:UXML ...>
    <engine:VisualElement class="portrait">
        <engine:Image name="portaitImage" style="--unity-image: url(\"a.png\")"/>
        <engine:Label name="nameLabel" text="Name"/>
        <engine:Label name="levelLabel" text="42"/>
    </engine:VisualElement>
</engine:UXML>

You can embed the Portrait component into other UXML templates like this:

<engine:UXML ...>
    <engine:Template src="/Assets/Portrait.uxml" name="Portrait"/>
    <engine:VisualElement name="players">
        <engine:Instance template="Portrait" name="player1"/>
        <engine:Instance template="Portrait" name="player2"/>
    </engine:VisualElement>
</engine:UXML>

Overriding UXML attributes

When you create instances of a UXML template, you can override the default attribute values of its elements. Attribute overrides allow you to instance the same template many times with different values for each instance.

To override an attribute, you must specify:

  • The element-name attribute of the element whose attributes you want to override
  • The name of the attribute to override
  • The new attribute value

In the following example,

  • player-name-label is the element’s element-name attribute.
  • text is the attribute to overrides.
  • Alice is the new attribute value
<AttributeOverrides element-name="player-name-label" text="Alice" />

Attribute overrides affect the entire instance, so if the instance has two elements named player-name-label, and both have text attributes, the override affects both of them.

Attribute override example

Let’s say you’re making a game, and you want to display the same set of information for each player. You could create a single reusable template, and use attribute overrides to create player-specific instances.

For example, if you create the following template:

    <UXML xmlns="UnityEngine.UIElements">
        <Label name="player-name-label" text="default name" />
        <Label name="player-score-label" text="default score" />
    </UXML>

You can instance it from another UXML file and override its attributes to display each player’s name and score:

    <UXML xmlns="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements">
        <Template src="MyTemplate.uxml" name="MyTemplate" />
        <Instance name="player1" template="MyTemplate">
            <AttributeOverrides element-name="player-name-label" text="Alice" />
            <AttributeOverrides element-name="player-score-label" text="2" />
        </Instance>
        <Instance name="player2" template="MyTemplate">
            <AttributeOverrides element-name="player-name-label" text="Bob" />
            <AttributeOverrides element-name="player-score-label" text="1" />
        </Instance>
    </UXML>

Overriding multiple attributes

You can specify more than one attribute per override. For example, the following syntax finds any element in the instance named player-name-label, and

  • Overrides the default value of its text attribute with the new value, Alice.
  • Overrides the default value of its tooltip attribute with the new value, Tooltip 1.
<AttributeOverrides element-name="player-name-label" text="Alice" tooltip="Tooltip 1" />

Nesting attribute overrides

Attribute overrides propagate through nested templates in the element hierarchy. For example, if template A instances template B, and template B instances template C, both template A and template B can override attributes in template C.

When you override attributes in nested templates, the deepest override takes precedence. So in the above example if template A and template B both override the same attribute of template C, the override in template B determines what actually appears in the rendered UI.

Limitations

  • Attribute overrides find matching attributes according to the element name you specify. They do not use USS Selectors or UQUery to match elements.
  • Attribute overrides are not tied to data binding, although you can override an element’s binding-path attribute.
  • You cannot override an element’s name or style attributes.

Referencing other files from UXML

UXML files can reference other UXML files and USS files via the element.

Both the <Template> and the <Style> elements accept either an “src” attribute or a “path” attribute.

The src attribute allows relative paths, provides error messages at import time (for example, when files are missing), and ensures that Unity properly includes the Assets your UXML files reference in player builds.

The path attribute permits usage of Unity Resources mechanisms, but does not provide error reporting at import time, and does not allow relative paths.

The src attribute

The src attribute expects the file path to be relative to either the project root or the folder containing the UXML file. You must include the file extension. In the following examples, the UXML files are located at Assets\Editor\UXML and the USS file is located at Assets\Editor\USS.

  • Use one of the following examples path, based on the location of the UXML file: src="../USS/styles.uss" or src="template.uxml"
  • Use one of the following examples for an absolute path, based on the location of the project: src="/Assets/Editor/USS/styles.uss" or src="project:/Assets/Editor/UXML/template.uxml".

The path attribute

The path attribute accepts files located in either the Resources folder or the Editor Default Resources folder, with the following rules:

  • If the file is in the Resources folder, do not include the file extension. For example, write path="template" for a file located at Assets/Resources/template.uxml.
  • If the file is in the Editor Default Resources folder, you must include the file extension. For example, write path="template.uxml" for a file located at Assets/Editor Default Resources/template.uxml.

  • 2018–11–02 Page amended
The UXML format
Loading UXML from C#