UI Switch
Updated
An on/off switch.
- Type
ent-ui-switch- Script class
UISwitchEntity- Family
- UI
Overview
Section titled “Overview”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.
Set it up
Section titled “Set it up”- Add a UI Switch under a UI Panel or UI Layout, one per setting. See Scenes and hierarchy.
- Set its
textand whether it startschecked. - Add a Signal Emitter with a
signal, so it reports each change. - In a Behavior, listen with
on(signal, …)and readevent.data.checked.
Examples
Section titled “Examples”A room light switch
Section titled “A room light switch”Goal: a switch on a wall panel turns a lamp on and off.
#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, andsetEnabledturns 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.colorpaints the track yellow while it is on.
A setting that unlocks another
Section titled “A setting that unlocks another”Goal: subtitles have a size slider that only works while subtitles are on.
#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:
applyreads the current state of both controls and dims the slider withdisabledwhenever subtitles are off.- The slider reports
UISlider::change-endonce 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.
Turn every switch off
Section titled “Turn every switch off”Goal: a Reset button turns off every switch in a panel.
#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;
isEntityskips 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 ownchangeevent, so any script listening to it reacts as if the player had flipped it.
Components
Section titled “Components”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- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| UI Switch | comp- |
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 |
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.
UISwitch::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.
An AI Memory names the record that keeps the guide's memory between visits: a UI Switch turns it on and off, and the agent's memory activity events update a label.
Flip Remember me: the label shows whether the guide keeps a memory, and in which record. Reading and writing memory happen during replies, which need the AI service; this preview does not have it, so a message you send shows a short error instead. Drag outside the panels to orbit the camera.
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 Switch.
#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);}