Version: 2019.1
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 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).

Reusing UXML

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:

<?xml version="1.0" encoding="utf-8"?>
<engine:UXML ...>
    <engine:VisualElement class="portrait">
        <engine:Image name="portaitImage" image="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:

<?xml version="1.0" encoding="utf-8"?>
<engine:UXML ...>
    <engine:Template path="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>

The UXML format
Loading UXML from C#