You can create a runtime UI and display it in a Game view by the following steps:
.uxml) with controls.MonoBehaviours to define the behavior of your UI controls.Try the following simple runtime UI example to get started. The example adds a label, a button, a toggle, and a text field in a scene. When you click the button, the Console window shows a message. When you select the toggle and click the button, the Console window shows how many times the buttons have been clicked. When you enter a text message in the text field, the Console window shows the message.
This guide is for developers familiar with the Unity Editor, UI Toolkit, and C# scripting. Before you start, get familiar with the following:
You can find the completed files that this example creates in this GitHub repository.
Create a UXML Document with a label, a button, and a Toggle. For information on how to add UI controls with UI Builder or UXML, refer to Get started with UI Toolkit.
SimpleRuntimeUI.uxml with the following contents:<ui:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd">
<ui:VisualElement style="flex-grow: 1;">
<ui:Label text="This is a Label" display-tooltip-when-elided="true" />
<ui:Button text="This is a Button" display-tooltip-when-elided="true" name="button" />
<ui:Toggle label="Display the counter?" name="toggle" />
<ui:TextField picking-mode="Ignore" label="Text Field" name="input-message" value="filler text" />
</ui:VisualElement>
</ui:UXML>
Create a Panel Renderer GameObject in the SampleScene and add the UXML Document as the source asset.
In the SampleScene, select GameObject > UI Toolkit > Panel Renderer. This creates the following:
Select the Panel Renderer GameObject in the hierarchy and drag SimpleRuntimeUI.uxml from your Project window to the Source Asset field of the Panel Renderer component in the Inspector window. This references the source asset to the UXML file you created.
To add logic, create a C# script that derives from MonoBehaviour to access the controls that the UI Document component references.
Unity loads a UI Document component’s source UXML when OnEnable is called on the component. To ensure the visual tree is loaded correctly, add logic to interact with the controls inside the OnEnable method.
Create a C# script named SimpleRuntimeUIWithUXML.cs with the following contents:
using UnityEngine;
using UnityEngine.UIElements;
public class SimpleRuntimeUIWithUXML : MonoBehaviour
{
private Button button;
private Toggle toggle;
private int clickCount;
void Awake()
{
GetComponent<PanelRenderer>().RegisterUIReloadCallback(OnUIReload);
}
void OnDestroy()
{
GetComponent<PanelRenderer>().UnregisterUIReloadCallback(OnUIReload);
}
private void OnUIReload(PanelRenderer panelRenderer, VisualElement root, int version)
{
button = root.Q<Button>("button");
toggle = root.Q<Toggle>("toggle");
button.RegisterCallback<ClickEvent>(PrintClickMessage);
var inputFields = root.Q<TextField>("input-message");
inputFields.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}");
}
}
Add SimpleRuntimeUIWithUXML.cs as a component of the Panel Renderer GameObject.
In the Panel Renderer inspector, set the “Source Asset” to the SimpleRuntimeUI.uxml document created earlier.