You can create a UXML file as a template and reuse it in other UXML files.
When you design a large user interface, you can create template UXML files that define parts of the UI, and use the <Template>
and <Instance>
elements to import it into another UXML file.
For example, if you have a portrait UI(User Interface) Allows a user to interact with your application. Unity currently supports three UI systems. More info
See in Glossary element that has an image, a name, and a label, you can create a UXML template file as Assets/Portrait.uxml
with the following content:
<ui:UXML ...> <ui:VisualElement class="portrait"> <ui:Image name="portaitImage" style="--unity-image: url(\"a.png\")"/> <ui:Label name="nameLabel" text="Name"/> <ui:Label name="levelLabel" text="42"/> </ui:VisualElement> </ui:UXML>
You can then reuse the Portrait template like this:
<ui:UXML ...> <ui:Template src="/Assets/Portrait.uxml" name="Portrait"/> <ui:VisualElement name="players"> <ui:Instance template="Portrait" name="player1"/> <ui:Instance template="Portrait" name="player2"/> </ui:VisualElement> </ui:UXML>
When you create instances of a UXML template, you can override the default attribute values of its elements. Attribute overrides allow you to instantiate the same template many times with different values for each instance.
You can override attributes with the UXML
tag. To override an attribute, specify the following:
element-name
attribute of the element whose attributes you want to overrideFor example, if you want to display the same set of information for each player in your game, you can create a UXML template, and use attribute overrides to create player-specific instances.
First, create a template, such as MyTemplate.uxml
, with the following content:
<UXML xmlns="Unityui.UIElements"> <Label name="player-name-label" text="default name" /> <Label name="player-score-label" text="default score" /> </UXML>
Then, 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"> <!-- Alice is the new value of the text attribute for the player-name-label --> <AttributeOverrides element-name="player-name-label" text="Alice" /> <!-- 2 is the new value of the text attribute for the player-score-label --> <AttributeOverrides element-name="player-score-label" text="2" /> </Instance> <Instance name="player2" template="MyTemplate"> <!-- Bob is the new value of the text attribute for the player-name-label --> <AttributeOverrides element-name="player-name-label" text="Bob" /> <!-- 1 is the new value of the text attribute for the player-score-label --> <AttributeOverrides element-name="player-score-label" text="1" /> </Instance> </UXML>
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
text
attribute with the new value, Alice
.tooltip
attribute with the new value, Tooltip 1
.<AttributeOverrides element-name="player-name-label" text="Alice" tooltip="Tooltip 1" />
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 shallowest override takes precedence. In the example above, if template A and template B both override the same attribute of template C, the override in template A determines what actually appears in the rendered UI.
If you’re creating instances of a UXML template, and an element in the template has an inline style defined with the style
attribute, you can’t use AttributeOverrides
to override that style
attribute. However, you can use USS selectors in a USS style sheet to override the styling of your template instances.
For example, if you have the following UXML template called Hotkeys.uxml
that defines a #Container
with two labels, and the #Container
has an inline style that defines the flex row direction:
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" editor-extension-mode="False"> <ui:VisualElement name="Container" style="flex-direction: row;"> <ui:Label text="E" name="Hotkeys" /> <ui:Label text="Talk" name="Action" /> </ui:VisualElement> </ui:UXML>
If you want to create two template instances with the second having a reversed flex row direction, you can’t use AttributeOverides
to override the style
attribute of the #Container
element in your second instance.
To override the style, do the following:
HotkeysXML
and ReversedHotkeysXML
.ContextHotkeys.uss
, to your UXML instance file.<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements"> <ui:Template name="HotkeysXML" src="HotkeysXML.uxml"/> <Style src="ContextHotKeys.uss"/> <ui:Instance template="HotkeysXML" name="HotkeysXML" /> <ui:Instance template="HotkeysXML" name="ReversedHotkeysXML" /> </ui:UXML>
You can then create ContextHotkeys.uss
to change the #Container
style according to the template instance name:
#ReversedHotkeysXML > #Container { flex-direction: row-reverse; } #HotkeysXML > #Container { flex-direction: row; }
Attribute overrides have the following limitations:
binding-path
attribute, data binding doesn’t work with attribute overrides.class
, name
, or style
attributes.You can use the content-container
attribute of a visual elementA node of a visual tree that instantiates or derives from the C# VisualElement
class. You can style the look, define the behaviour, and display it on screen as part of the UI. More info
See in Glossary to specify where to nest child elements in a UXML template. For example, if you have the following UXML template file as Assets/MyTemplate.uxml
:
<ui:UXML xmlns:ui="UnityEngine.UIElements" ...> <ui:Label text="Group Title" name="groupTitle" /> <ui:VisualElement name="group-container" content-container="anyValue"> <!--Add child elements here --> </ui:VisualElement> <ui:VisualElement /> </ui:UXML>
You can then apply the template with nested child elements as this:
<ui:UXML xmlns:ui="UnityEngine.UIElements" ...> <ui:Template path="Assets/MyTemplate.uxml" name="my-template"/> <ui:Instance template="my-template"> <ui:Label text="Test"/> <!--This label element is instantiated inside the `group-container` element--> </ui:Instance> </ui:UXML>
Note: You can provide any value to the content-container
attribute.
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.
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.