UI Toggle Button
Updated
A button that stays on until clicked again.
- Type
ent-ui-toggle-button- Script class
UIToggleButtonEntity- Family
- UI
Overview
Section titled “Overview”A UI Toggle Button is a button that latches. A press turns it on and its fill stays lit; the next press turns it off. Its UI Toggle Button component (comp-ui-toggle-button) keeps that state in checked and sends UIToggleButton::change with { checked } each time it flips. A new toggle button shows the text Toggle.
It looks like a UI Button, with a label, an icon or both, and sits nested in a panel or layout, usually in a toolbar. It can also stand alone as a world-space root, for example a single power button beside a machine.
Three controls hold an on/off state. Choose by what the player is doing:
- UI Toggle Button: a mode or tool that is active, shown as a lit button. Microphone on, flashlight on, the brush picked in a paint toolbar. With a UI Selection Group, several toggle buttons act as a tool picker where only one is lit.
- UI Switch: a setting that is on or off, with a label beside a sliding track. Music, subtitles, invert look.
- UI Checkbox: an option in a list that the player ticks, or with
radio: true, one choice among several.
The props that matter most:
textandicon: the label and an icon name.checked: whether it starts on.backgroundColorandcheckedColor: the fill while off and while on.disabled: dims the button and ignores presses.
From a script, isChecked() reads the state, and setChecked(checked) and toggle() change it. Both send UIToggleButton::change, exactly as a press does.
Set it up
Section titled “Set it up”- Add a UI Toggle Button under a UI Panel or UI Layout; for a toolbar, under a layout with
flexDirection: 'row'. See Scenes and hierarchy. - Set its
text(oricon) and whether it startschecked. - Add a Signal Emitter with a
signal, so it reports each change. - For a set where only one may be on, add a UI Selection Group to each toggle button with the same
groupname.
Examples
Section titled “Examples”A flashlight switch
Section titled “A flashlight switch”Goal: a toggle button turns a light on and off, and its label says what it does next.
#pragma lifecycle(startup)
// Scene: a light entity named 'Flashlight', and a UI Toggle Button named 'Light Toggle'// with a Signal Emitter on 'hud:light'.function startup() { const light = findEntity('Flashlight'); const toggle = findEntity('Light Toggle').getComponent(UIToggleButtonComponent);
function apply(on) { light.setEnabled(on); toggle.setProps({ text: on ? 'Light off' : 'Light on' }); }
on('hud:light', (event) => { if (event.eventType === EventType.UIToggleButton.Change) apply(event.data.checked); });
apply(toggle.isChecked());}How it works:
UIToggleButton::changecarries the new state inevent.data.checked.setEnabledon the light entity turns the light on or off.- The last line applies the button’s starting state once, so the light matches the button even before the first press.
A tool picker
Section titled “A tool picker”Goal: three toggle buttons pick a paint tool; only one is lit at a time.
#pragma lifecycle(startup)
// Scene: a row of three UI Toggle Buttons named 'Brush', 'Eraser' and 'Fill',// each with a Signal Emitter on 'paint:tool'.const TOOLS = ['Brush', 'Eraser', 'Fill'];let tool = 'Brush';
function startup() { for (const name of TOOLS) { const entity = findEntity(name); entity.getComponent(UIToggleButtonComponent).setProps({ text: name }); entity.addComponent(new UISelectionGroupComponent({ group: 'paint-tool' })); } findEntity(tool).getComponent(UIToggleButtonComponent).setChecked(true);
on('paint:tool', (event) => { if (event.eventType !== EventType.UIToggleButton.Change || !event.data.checked) return; tool = findEntity(event.entityId).name; emit('paint/tool', { tool }); });}How it works:
- A UI Selection Group with the same
groupmakes the buttons exclusive: turning one on turns the others off. setChecked(true)from the script applies the group too, so the starting tool is the only lit one.- The listener acts only on
checked: true, from the button that was turned on, and reads which button that is fromevent.entityId. A press on the lit button turns it off; the group never forces one back on.
A mute button with a count
Section titled “A mute button with a count”Goal: a microphone toggle that shows how many times the player has been muted by a moderator, using the badge.
#pragma lifecycle(startup)
// Scene: a UI Toggle Button named 'Mic' with a Signal Emitter on 'hud:mic'.// Other scripts emit 'mod/mute' when a moderator mutes the player.let mutes = 0;
function startup() { const mic = findEntity('Mic').getComponent(UIToggleButtonComponent); mic.setProps({ text: 'Mic', checkedColor: '#16a34a' });
on('hud:mic', (event) => { if (event.eventType !== EventType.UIToggleButton.Change) return; emit('voice/enabled', { on: event.data.checked }); });
on('mod/mute', () => { mutes += 1; mic.setBadge(mutes); if (mic.isChecked()) mic.setChecked(false); });}How it works:
checkedColorsets the fill while the button is on: green while the microphone is live.- When a moderator mutes the player,
setChecked(false)turns the button off. That sends achangeevent, so the first listener reportson: falsewithout extra code. setBadge(mutes)counts the moderator mutes in the badge.
Components
Section titled “Components”What a new UI Toggle Button is made of. Required components are always present and cannot be removed; the others can. Per entity says whether an entity can hold one of that component or several. Layer, Lock and Enable are on every placed entity, and Transform holds its position, rotation and scale.
| Component | Type | Role | Per entity | What it does |
|---|---|---|---|---|
| Layer | comp-layer |
Required | One | Puts the entity on a render/raycast layer so cameras and rays can include or skip it. |
| Lock | comp-lock |
Required | One | Editor-only flag that stops the entity from being selected or moved. |
| Enable | comp-enable |
Required | One | Turns the entity and its children on or off: hides them and stops their behavior. |
| Transform | comp- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| UI Toggle Button | comp- |
Required | One | A button that stays on until clicked again. |
You can also add 5 optional components, such as Networkable, Networkable Trait, Signal Emitter, UI Selection Group, UI Tooltip.
The settings of a new entity, as the runtime creates it. Change them in the Properties panel of XR Creator, or from a script with getComponent(…).setProps({ … }).
| Prop | Default | Range |
|---|---|---|
text |
"Toggle" |
|
iconPosition |
"left" |
|
align |
"center" |
|
disabled |
false |
|
checked |
false |
Events
Section titled “Events”With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.
UIToggleButton::change
Like every entity, it also reports: Transform::change (and position-change, rotation-change, scale-change), Enable::change, Layer::change and Lock::change.
Live example
Section titled “Live example”Run it in the browser, read its scripts, and download it as an XR Creator project.
A UI Video in a panel with a UI Toggle Button that plays and pauses it and a volume slider; the button's label follows the video's own play and pause events.
Press Play to start the video with sound, press again to pause, and drag the slider to change the volume. Drag outside the panel to orbit the camera, scroll to zoom.
From a script
Section titled “From a script”Find the entity by its name, check its class, and work with its components. Name the entity in the Hierarchy first; this example expects My UI Toggle Button.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My UI Toggle Button'); if (!(entity instanceof UIToggleButtonEntity)) return;
// Its UI Toggle Button component holds the settings listed under Props. const component = entity.getComponent(UIToggleButtonComponent); log(component.props);}