Skip to content

UI Toggle Button

Updated

A button that stays on until clicked again.

Type
ent-ui-toggle-button
Script class
UIToggleButtonEntity
Family
UI

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:

  • text and icon: the label and an icon name.
  • checked: whether it starts on.
  • backgroundColor and checkedColor: 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.

  1. 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.
  2. Set its text (or icon) and whether it starts checked.
  3. Add a Signal Emitter with a signal, so it reports each change.
  4. For a set where only one may be on, add a UI Selection Group to each toggle button with the same group name.

Goal: a toggle button turns a light on and off, and its label says what it does next.

Flashlight.jx
#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::change carries the new state in event.data.checked.
  • setEnabled on 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.

Goal: three toggle buttons pick a paint tool; only one is lit at a time.

ToolPicker.jx
#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 group makes 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 from event.entityId. A press on the lit button turns it off; the group never forces one back on.

Goal: a microphone toggle that shows how many times the player has been muted by a moderator, using the badge.

MicToggle.jx
#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:

  • checkedColor sets 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 a change event, so the first listener reports on: false without extra code.
  • setBadge(mutes) counts the moderator mutes in the badge.

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-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
UI Toggle Button comp-ui-toggle-button 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

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.

Run it in the browser, read its scripts, and download it as an XR Creator project.

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.

UIToggleButtonEntity.jx
#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);
}