Documentation

Unity Engine


User Manual

Script Reference

Unity Engine


UIDocument

Defines a Component that connects VisualElements to GameObjects.
Read time 8 minutesLast updated 5 days ago

Definition

[AddComponentMenu("UI Toolkit/Legacy/UI Document (UI Toolkit)")][HelpURL("UIE-get-started-with-runtime-ui")][DefaultExecutionOrder(-100)]public sealed class UIDocument : MonoBehaviour, IPanelComponent

Remarks

This makes it possible to render UI defined in UXML documents in the Game view.
The following example shows how to query a UIDocument component and interact with its elements.

Examples

using UnityEngine;using UnityEngine.UIElements;public class UIDocument_Example : MonoBehaviour{ // Configured via the inspector public UIDocument document; void OnEnable() { // Expects a button with the name "my-button" to be present in the UXML. // If it doesn't exist, create it. var button = document.rootVisualElement.Q<Button>("my-button"); if (button == null) { button = new Button { name = "my-button", text = "Click me!" }; document.rootVisualElement.Add(button); } button.clicked += ButtonClicked; } void ButtonClicked() { var label = document.rootVisualElement.Q<Label>("my-label"); // Add a label if it doesn't exist if (label == null) { label = new Label { name = "my-label", text = "Button clicked!" }; document.rootVisualElement.Add(label); } else { label.text = "Button clicked!"; } }}

Properties

Property

Description

panelSettingsSpecifies the PanelSettings instance to connect this UIDocument component to.
parentUIIf the GameObject that this UIDocument component is attached to has a parent GameObject, and that parent GameObject also has a UIDocument component attached to it, this value is set to the parent GameObject's UIDocument component automatically.
pivotDefines the pivot point for positioning and transformation, such as rotation and scaling, of the UI Document in world space. The default pivot is the center.
pivotReferenceSizeDefines how the size of the container is calculated for pivot positioning.
positionThe position (relative or absolute) of the root visual element. Relative only applies for nested UIDocuments.
rootVisualElementThe root visual element where the UI hierarchy starts.
runtimePanelThe runtime panel whose visualTree contains this document's rootVisualElement, if any.
sortingOrderThe order in which this UIDocument will show up on the hierarchy in relation to other UIDocuments either attached to the same PanelSettings, or with the same UIDocument parent.
visualTreeAssetThe VisualTreeAsset loaded into the root visual element automatically.
worldSpaceSizeWhen the _worldSpaceSizeMode is set to UIDocument.WorldSpaceSizeMode.Fixed, this property determines the size of the UIDocument in world space.
worldSpaceSizeModeDefines how the size of the root element is calculated for world space.

Static Methods

Method

Description

FindUIDocumentFinds the UIDocument whose UI hierarchy contains the given element.

Inheritance

Inherited Members

Operators

Operator

Description

operator ==Compares two object references to see if they refer to the same object.
boolDetermines whether the object exists.
operator !=Compares if two objects refer to a different object.