UIDocument
Defines a Component that connects VisualElements to GameObjects.
Read time 8 minutesLast updated 5 days ago
Definition
- Type: Class
- Namespace: UnityEngine.UIElements
- Assembly: UnityEngine.UIElementsModule
- Inherits from: MonoBehaviour
- Implements: IPanelComponent
[HelpURL("UIE-get-started-with-runtime-ui")][AddComponentMenu("UI Toolkit/Legacy/UI Document (UI Toolkit)")][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 |
|---|---|
| panelSettings | Specifies the PanelSettings instance to connect this UIDocument component to. |
| parentUI | If 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. |
| pivot | Defines 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. |
| pivotReferenceSize | Defines how the size of the container is calculated for pivot positioning. |
| position | The position (relative or absolute) of the root visual element. Relative only applies for nested UIDocuments. |
| rootVisualElement | The root visual element where the UI hierarchy starts. |
| runtimePanel | The runtime panel whose visualTree contains this document's rootVisualElement, if any. |
| sortingOrder | The 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. |
| visualTreeAsset | The VisualTreeAsset loaded into the root visual element automatically. |
| worldSpaceSize | When the _worldSpaceSizeMode is set to UIDocument.WorldSpaceSizeMode.Fixed, this property determines the size of the UIDocument in world space. |
| worldSpaceSizeMode | Defines how the size of the root element is calculated for world space. |
Inheritance
Operators
Operator | Description |
|---|---|
| operator == | Compares two object references to see if they refer to the same object. |
| bool | Determines whether the object exists. |
| operator != | Compares if two objects refer to a different object. |