Skip to content

UI Switch

Updated

An on/off switch.

Type
ent-ui-switch
Script class
UISwitchEntity
Family
UI

A UI Switch is an on/off switch: a pill-shaped track with a handle that slides across, and a label beside it. Its UI Switch component (comp-ui-switch) keeps the state in checked and sends UISwitch::change with { checked } each time it flips. A press counts when it is released over the switch’s row; releasing elsewhere cancels it. A new switch shows the label Switch.

Switches sit nested in a panel or layout, typically one per row in a settings panel.

Three controls hold an on/off state. Choose by what the player is doing:

  • UI Switch: a setting that takes effect at once, like a light switch: music, subtitles, invert look, show the minimap.
  • UI Checkbox: an option the player ticks, often one of several in a list, or a choice confirmed later with a button.
  • UI Toggle Button: a mode or tool shown as a lit button, typically in a toolbar.

The props that matter most:

  • text: the label beside the track. An explicitly empty text draws no label.
  • checked: whether it starts on.
  • color: the track colour while on.
  • disabled: dims the switch and ignores presses.

From a script, isChecked() reads the state, and setChecked(checked) and toggle() change it. Both send UISwitch::change, exactly as a press does.

  1. Add a UI Switch under a UI Panel or UI Layout, one per setting. See Scenes and hierarchy.
  2. Set its text and whether it starts checked.
  3. Add a Signal Emitter with a signal, so it reports each change.
  4. In a Behavior, listen with on(signal, …) and read event.data.checked.

Goal: a switch on a wall panel turns a lamp on and off.

LampSwitch.jx
#pragma lifecycle(startup)
// Scene: a light entity named 'Ceiling Lamp', and a UI Switch named 'Lamp'
// with a Signal Emitter on 'room:lamp'.
function startup() {
const lamp = findEntity('Ceiling Lamp');
const toggle = findEntity('Lamp').getComponent(UISwitchComponent);
toggle.setProps({ text: 'Ceiling lamp', color: '#facc15' });
on('room:lamp', (event) => {
if (event.eventType === EventType.UISwitch.Change) lamp.setEnabled(event.data.checked);
});
toggle.setChecked(lamp.isEnabled());
}

How it works:

  • Each flip arrives as UISwitch::change, and setEnabled turns the lamp entity on or off to match.
  • setChecked(lamp.isEnabled()) starts the switch in the same state as the lamp, so the first press always does what the player expects.
  • color paints the track yellow while it is on.

Goal: subtitles have a size slider that only works while subtitles are on.

Subtitles.jx
#pragma lifecycle(startup)
// Scene: a settings panel with a UI Switch named 'Subtitles' (Signal Emitter: 'settings:subtitles')
// and a UI Slider named 'Subtitle Size' (Signal Emitter: 'settings:subtitle-size').
function startup() {
const subtitles = findEntity('Subtitles').getComponent(UISwitchComponent);
const size = findEntity('Subtitle Size').getComponent(UISliderComponent);
size.setProps({ text: 'Size', min: 12, max: 40, step: 2, unit: 'px' });
function apply() {
const on = subtitles.isChecked();
size.setProps({ disabled: !on });
emit('subtitles/settings', { on, size: size.getValue() });
}
on('settings:subtitles', (event) => {
if (event.eventType === EventType.UISwitch.Change) apply();
});
on('settings:subtitle-size', (event) => {
if (event.eventType === EventType.UISlider.ChangeEnd) apply();
});
apply();
}

How it works:

  • apply reads the current state of both controls and dims the slider with disabled whenever subtitles are off.
  • The slider reports UISlider::change-end once the player lets go, so the settings are sent once per drag rather than on every step.
  • emit('subtitles/settings', …) hands the result to whichever script draws the subtitles.

Goal: a Reset button turns off every switch in a panel.

ResetSwitches.jx
#pragma lifecycle(startup)
// Scene: a UI Panel named 'Options' whose direct children include several UI Switches,
// and a UI Button named 'Reset' with a Signal Emitter on 'options:reset'.
function startup() {
const options = findEntity('Options');
on('options:reset', (event) => {
if (event.eventType !== EventType.UIButton.Press) return;
for (const child of options.children) {
if (!isEntity(child)) continue;
const toggle = child.getComponent(UISwitchComponent);
if (toggle && toggle.isChecked()) toggle.setChecked(false);
}
});
}

How it works:

  • The loop walks the panel’s children; isEntity skips anything that is not an entity.
  • getComponent(UISwitchComponent) returns nothing for children that are not switches, so labels and buttons are left alone.
  • Each setChecked(false) sends that switch’s own change event, so any script listening to it reacts as if the player had flipped it.

What a new UI Switch 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 Switch comp-ui-switch Required One An on/off switch.

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 "Switch"
checked false
disabled false

With a Signal Emitter on the entity, these events reach scripts as { entityId, eventType, data }. Compare eventType with the EventType constants.

  • UISwitch::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 Switch.

UISwitchEntity.jx
#pragma lifecycle(startup)
function startup() {
// Find the entity by the name it has in the Hierarchy.
const entity = findEntity('My UI Switch');
if (!(entity instanceof UISwitchEntity)) return;
// Its UI Switch component holds the settings listed under Props.
const component = entity.getComponent(UISwitchComponent);
log(component.props);
}