Version: 2019.4
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"?>
    xsi:schemaLocation="UnityEngine.UIElements ../UIElementsSchema/UnityEngine.UIElements.xsd">

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

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 UIElements, 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 > UIElements 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" />
#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;" />

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"/>

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"/>

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#