PanelRenderer
Defines a component that connects VisualElement to GameObject.
Read time 10 minutesLast updated 6 days ago
Definition
- Type: Class
- Namespace: UnityEngine.UIElements
- Assembly: UnityEngine.UIElementsModule
- Inherits from: Renderer
- Implements: IPanelComponent
[HelpURL("ui-systems/panel-renderer-component")][AddComponentMenu("UI Toolkit/Panel Renderer (UI Toolkit)")]public sealed class PanelRenderer : Renderer, IPanelComponent
Remarks
The following example uses the Panel Renderer component to display a runtime UI in a scene:
Examples
using UnityEngine;using UnityEngine.UIElements;public class SimpleRuntimeUI : MonoBehaviour{ Button button; Toggle toggle; int clickCount = 0; int uiVersion = 0; void Awake() { GetComponent<PanelRenderer>().RegisterUIReloadCallback(OnUIReload); } void OnDestroy() { GetComponent<PanelRenderer>().UnregisterUIReloadCallback(OnUIReload); } private void OnUIReload(PanelRenderer panelRenderer, VisualElement root, int version) { // The version number only changes when the UI actually reloads, // so this checks prevents duplicated elements. if (uiVersion == version) return; uiVersion = version; button = new Button() { text = "Click me!" }; root.Add(button); button.RegisterCallback<ClickEvent>(PrintClickMessage); toggle = new Toggle() { text = "Show click count" }; root.Add(toggle); var input = new TextField() { label = "Type something:" }; root.Add(input); input.RegisterCallback<ChangeEvent<string>>(InputMessage); } private void PrintClickMessage(ClickEvent evt) { ++clickCount; Debug.Log($"{"button"} was clicked!" + (toggle.value ? " Count: " + clickCount : "")); } public static void InputMessage(ChangeEvent<string> evt) { Debug.Log($"{evt.newValue} -> {evt.target}"); }}
Properties
Property | Description |
|---|---|
| panelSettings | Specifies the PanelSettings instance to connect this PanelRenderer component to. |
| parentUI | If the GameObject that this PanelRenderer component is attached to has a parent GameObject, and that parent GameObject also has a PanelRenderer component attached to it, this value is set to the parent GameObject's PanelRenderer 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 PanelRenderers. |
| visualTreeAsset | The VisualTreeAsset automatically loaded into the root visual element. |
| worldSpaceSize | When the _worldSpaceSizeMode is set to WorldSpaceSizeMode.Fixed, this property determines the size of the PanelRenderer in world space. |
| worldSpaceSizeMode | Defines how the size of the root element is calculated for world space. |
Methods
Method | Description |
|---|---|
| RegisterUIReloadCallback | Registers a callback to be invoked when the UI is reloaded. |
| UnregisterUIReloadCallback | Unregisters a previously registered UI reload callback. |
Delegates
Delegate | Description |
|---|---|
| PanelRenderer.UIReloadCallback | Callback type for UI reload events. |
| PanelRenderer.VersionedUIReloadCallback | Callback type for UI reload events. |
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. |