Panel Renderer component
Create a Panel Renderer component to connect the UI to the panel.
読み終わるまでの所要時間 7 分最終更新 7日前
A Panel Renderer component references a UXML document and a Panel Settings asset. It serves as a bridge between the Scene and a UXML document. The UXML document specifies the UI structure, while the Panel Settings asset handles rendering.
Create a Panel Renderer component
To create a Panel Renderer component, from the menu, do one of the following:
- To add a Panel Renderer component to an existing GameObject in your scene, select Component > UI Toolkit > Panel Renderer.
- To create a new GameObject with a preconfigured Panel Renderer component, select > UI Toolkit > Panel Renderer.GameObject
?
Connect the UI to a panel
To Connect the UI to a panel, in the window of the Panel Renderer component, configure Panel Renderer component.
Inspector
?

Panel Renderer component
- Panel Settings: The Panel Settings asset that renders the UI.
- Source Asset: The UXML document that contains the UI to display.
- Sort Order: The rendering order of the Panel Renderer component. The lower the value, the earlier the Panel Renderer component renders. For more information about rendering order, refer to Render multiple UXML Documents on the same panel.
- World-Space Dimensions: If you enabled World Space rendering mode, specify the dimensions of the UI in World Space. For more information about World Space rendering, refer to Create a World Space UI.
Render multiple UXML Documents on the same panel
A panel can display UI from more than one Panel Renderer component. This allows you to break complex UIs into smaller, more manageable parts. Each Panel Renderer component references a different UXML document and the same Panel Settings asset.
Each Panel Renderer component has a Sort Order property that controls rendering order. The UXML documents are rendered based on their order in the Hierarchy: child Panel Renderer components render on top of their parents. Sibling Panel Renderer components (at the same hierarchy level) render in ascending order based on their Sort Order value. The lower the Sort Order value, the earlier the Panel Renderer component renders. To avoid unpredictable rendering, assign unique Sort Order values for siblings when precise layering is important.
Lifecycle of Panel Renderer components
The Panel Renderer lifecycle follows standard Unity component behavior, but it doesn't expose its root visual element directly. Instead, you register a callback that the Panel Renderer invokes when its UI is ready.
How the UI reload callback works
The Panel Renderer creates a root visual element and attaches it to the panel. If you assign a Source Asset, the Panel Renderer clones the UXML document into the root. The Source Asset is optional. Without one, the root is an empty container, and you can add elements to it from C# code. In both cases, the root visual element isn't ready in or , and might not be ready in . If the Panel Renderer is enabled and has a Panel Settings asset, the UI is ready before the first of your scripts.
AwakeOnEnableStartUpdateTo run code when the UI is ready, register a callback with . The callback receives a parameter that identifies the current visual tree of the Panel Renderer. The version belongs to the Panel Renderer, not to the callback. All callbacks registered on the same Panel Renderer receive the same version. If a script registers callbacks on several Panel Renderers, it must track the version of each Panel Renderer separately.
RegisterUIReloadCallbackversionThe following table lists when the Panel Renderer invokes the callback, and whether the version increases:
Situation | Version increases |
|---|---|
| The UI is first attached to the panel. | Yes |
| Live reload rebuilds the UI after you save a change to the UXML document, including in Play mode. | Yes |
| You assign a different Source Asset. | Yes |
| The component is re-enabled. | No |
| You register the callback after the UI is already attached to the panel. The Panel Renderer invokes the callback immediately. | No |
Changes to USS files restyle the existing elements and don't invoke the callback. Assigning a different Panel Settings asset also doesn't invoke the callback. For more information, refer to Enable and disable the Panel Renderer.
When live reload rebuilds the UI, the version bump lets your scripts query the new elements and register their event handlers again, so they keep working while you edit the UI.
The version increases only when the Panel Renderer rebuilds the visual tree. It stays the same when you disable and re-enable the component, because the Panel Renderer keeps its visual tree in memory. Compare the version with the last one you processed to skip work that's already done.
Initialize the UI from a script
The recommended pattern is to register the callback in and deregister it in . In the callback, query the elements you need, cache them in fields, and register your event handlers.
AwakeOnDestroyThe following example displays the player's health in a progress bar, and restores it to full health when the user clicks a button. For more examples, refer to Runtime UI examples.
using UnityEngine;using UnityEngine.UIElements;[RequireComponent(typeof(PanelRenderer))]public class HealthBarController : MonoBehaviour{ PanelRenderer m_PanelRenderer; ProgressBar m_HealthBar; int m_UIVersion; float m_Health = 100f; void Awake() { m_PanelRenderer = GetComponent<PanelRenderer>(); m_PanelRenderer.RegisterUIReloadCallback(OnUIReload); } void OnDestroy() { m_PanelRenderer.UnregisterUIReloadCallback(OnUIReload); } void OnUIReload(PanelRenderer panelRenderer, VisualElement root, int version) { // Skip the setup if the visual tree hasn't been rebuilt since the last call. if (version == m_UIVersion) return; m_UIVersion = version; m_HealthBar = root.Q<ProgressBar>("health"); root.Q<Button>("heal").clicked += () => m_Health = 100f; } void Update() { m_Health = Mathf.Max(0f, m_Health - 5f * Time.deltaTime); // The UI isn't ready yet if the Panel Renderer has never been enabled. if (m_HealthBar == null) return; m_HealthBar.value = m_Health; }}
When the Panel Renderer rebuilds the visual tree, the Panel Renderer discards the previous elements along with their event handlers. The callback then runs with a new version and a new root, and queries the new elements again.
Build UI in code
To build the UI entirely in C#, leave the Source Asset property empty and add elements to the root visual element in the callback. You can also add elements to a root that already contains a UXML document.
When your callback creates visual elements instead of querying them from a UXML document, use the same version check. The visual tree persists when you disable and re-enable the component, so elements that you add without the version check accumulate every time the callback runs.
void OnUIReload(PanelRenderer panelRenderer, VisualElement root, int version){ if (version == m_UIVersion) return; m_UIVersion = version; var button = new Button { text = "Play" }; button.clicked += StartGame; root.Add(button);}
Enable and disable the Panel Renderer
When you disable a Panel Renderer component, or deactivate its GameObject, Unity removes its visual elements from the panel but keeps them in memory. While disabled, the elements aren't rendered, don't receive input, and don't process events. You don't need to deregister element event handlers when you disable the component.
When you enable the component again, Unity adds the same visual elements back to the panel, in the state they were in when you disabled it. The version doesn't increase, so you don't need to create the elements or register their event handlers again. If you need to reset the UI when it reappears, such as restarting a transition or clearing a text field, do it from the callback or from your script's method.
OnEnableIf you assign a different Source Asset, the Panel Renderer rebuilds the UI and invokes the callback with a new version. If you assign a different Panel Settings asset, the Panel Renderer moves its existing visual elements to the new panel without rebuilding them, so the Panel Renderer doesn't invoke the callback and the version doesn't change. If the GameObject's parent changes, or if you nest the Panel Renderer under another Panel Renderer, the component updates its hierarchy and might reparent its root visual element accordingly.