Use UXML instances as templates in-scene
Use UXML documents as reusable templates inside other UXML documents and edit them in-scene or in UI Builder.
読み終わるまでの所要時間 4 分最終更新 9日前
You can instantiate existing UXML documents as templates inside another UXML document, similar to Prefabs. In the Hierarchy window, a template appears as a template instance of type .
TemplateContainerUse a UXML document as a template
To instantiate an existing UXML document:
- In the Project or the UI Library window, find the asset.
.uxml - While in UI Staging mode, drag it into the Hierarchy or the UI Viewport window.
A Template Instance appears in the Hierarchy window. Unity shows the source file name in grey beside the instance name. Expand the instance to view its internal hierarchy for reference. The internal hierarchy is read-only in the parent document.
Create a template from an element
To save an existing element and its children as a new UXML template:
- In the Hierarchy window, right-click (macOS: Ctrl+click) the element.
- Select Create Template.
- Choose a name and location for the new file.
.uxml
Unity exports the element and its children to the new file and replaces the original element with a Template Instance that references it. This action isn't fully reversible: Undo restores the element in the hierarchy but doesn't delete the created file.
.uxmlUnpack a template instance
Unpacking replaces a Template Instance with the child elements from the template placed directly in the parent document. Unity preserves inline attributes and child attribute overrides.
To unpack a template instance:
-
In the Hierarchy window, right-click (macOS: Ctrl+click) the template instance.
-
Select one of the following:
- Unpack Template: Expand the outermost template instance one level.
- Unpack Template Completely: Recursively expand all nested template instances.
Edit a template instance
To edit the UXML used by a template instance, right-click (macOS: Ctrl+click) the instance in the Hierarchy window and select one of the following:
- Open Instance in UI Builder > In Isolation: Load only the template. Parent document style sheets appear read-only in the Style Sheets window because they still apply to the instance.
- Open Instance > In Isolation: Load only the template and stay in UI Staging mode. Parent document style sheets appear read-only in the Style Sheets window.
- Open Instance in UI Builder > In Context: Keep the parent document visible (read-only) and edit the template in place.
- Open Instance > In Context: Edit the template in UI Staging mode while keeping parent context.
When you edit in context or isolation, a breadcrumb bar appears in the UI Viewport window. Use it to navigate back to parent documents.
You can also open a template from the window header when you select a Template Instance.
Inspector
?
Override attributes on a template instance
An attribute override changes an attribute on one template instance without changing the template's UXML asset. Unity stores the override in the parent document that contains the instance, in an element. Other instances of the template keep the template's value.
<AttributeOverrides>An element must have a name before you can override its attributes.
To override an attribute:
- In the Hierarchy window, select the element inside the template instance.
- In the Inspector window, right-click the attribute field and select Overrides. The submenu lists the parent documents that can override the attribute, and flags the ones that already override it.
- Select a parent document to open it in UI Staging mode, with the element selected and the attribute focused.
- In the parent document, set the attribute value.
If the element has no name, the Overrides submenu shows Give this element a name instead. Select it to move focus to the Name field in the Inspector window. If the element isn't inside a template instance, the submenu shows a disabled No .uxml above this element item.
Identify overridden attributes
Unity shows overrides in the following ways:
- In the Hierarchy window, a bar appears beside elements that have an attribute override from a parent document (white in the dark Editor theme, black in the light theme), similar to the blue bar for Prefab overrides. The bar marks only overrides, not attributes set to a non-default value.
- In the Main Stage, an attribute field that a parent document overrides is read-only. The Inspector window explains why, and names and links to the document that defines the override.
Change a template that has overrides
To change the template asset itself, right-click the template instance in the Hierarchy window and select Open Instance > In Isolation. Your changes apply to the template asset and reach every instance that doesn't override the changed attribute.