UI Slider
Updated
Drag to pick a value.
- Type
ent-ui-slider- Script class
UISliderEntity- Family
- UI
Overview
Section titled “Overview”A UI Slider picks a number by dragging. It shows a header with its label and current value, over a track with a coloured fill and a round thumb. Its UI Slider component (comp-ui-slider) keeps the value between min and max, snapped to step, and reports it to scripts. Pressing the track jumps to that point; dragging the thumb moves the value step by step, and the drag can continue outside the track. Mouse, touch and XR controller rays all drag it the same way.
Sliders sit nested in a panel or layout, usually in a settings panel or beside the thing they control.
A slider sends two events:
UISlider::change{ value }on every stepped change while the player drags, and when a script callssetValue. Use it for live feedback, such as a light that dims as the thumb moves.UISlider::change-end{ value }once the drag is released with a different value. Use it for work that should run once, such as saving a setting.
Use a UI Slider for a value the player chooses. To show progress the player does not control, use a UI Progress. For a plain on/off choice, use a UI Switch.
The props that matter most:
min,maxandstep: the range (0 to 100 by default) and the step the value snaps to (1 by default;0for a continuous value).value: the current value, kept within the range.text,showValueandunit: the header label, whether the value is shown, and a unit after it such as'%'.color: the fill and thumb colour.disabled: dims the slider and ignores input.
From a script, getValue() reads the value and setValue(value) changes it.
Set it up
Section titled “Set it up”- Add a UI Slider under a UI Panel or UI Layout. See Scenes and hierarchy.
- Set
text,min,max,stepand a startingvalue; add aunitif it helps. - Add a Signal Emitter with a
signal, so it reports changes. - In a Behavior, listen with
on(signal, …); handleEventType.UISlider.Changefor live updates andEventType.UISlider.ChangeEndfor once-per-drag work.
Examples
Section titled “Examples”A dimmer for a lamp
Section titled “A dimmer for a lamp”Goal: drag a slider to change a point light’s brightness as the thumb moves.
#pragma lifecycle(startup)
// Scene: a Point Light entity named 'Desk Lamp', and a UI Slider named 'Brightness'// with a Signal Emitter on 'lamp:brightness'.function startup() { const light = findEntity('Desk Lamp').getComponent(PointLightComponent); const slider = findEntity('Brightness').getComponent(UISliderComponent);
slider.setProps({ text: 'Brightness', min: 0, max: 100, step: 5, unit: '%' });
on('lamp:brightness', (event) => { if (event.eventType === EventType.UISlider.Change) { light.setProps({ intensity: (event.data.value / 100) * 2 }); } });
slider.setValue(50);}How it works:
setPropssets the range and a step of 5, andunit: '%'shows the value as a percentage in the header.- Every stepped change maps the 0–100 value to an intensity of 0 to 2 on the light.
setValue(50)starts the slider at half. It comes after the listener, and a script’ssetValuesendschangejust as a drag does, so the light takes the matching intensity from the start.
Live preview, save on release
Section titled “Live preview, save on release”Goal: show the field of view while dragging, and announce it once when the player lets go.
#pragma lifecycle(startup)
// Scene: a UI Slider named 'FOV' (Signal Emitter: 'settings:fov') and a UI Label named 'FOV Note'.function startup() { const fov = findEntity('FOV').getComponent(UISliderComponent); const note = findEntity('FOV Note').getComponent(UILabelComponent);
fov.setProps({ text: 'Field of view', min: 60, max: 110, step: 1, unit: '°' });
on('settings:fov', (event) => { if (event.eventType === EventType.UISlider.Change) { note.setProps({ text: event.data.value > 95 ? 'Wide: may feel fast' : '' }); } if (event.eventType === EventType.UISlider.ChangeEnd) { emit('settings/saved', { fov: event.data.value }); } });}How it works:
- One signal carries both events, so the listener compares
eventTypeto tell them apart. changeupdates the note on every step of the drag.change-endarrives once, when the drag is released with a new value, so the setting is announced once rather than dozens of times per drag.
A precise slider made by a script
Section titled “A precise slider made by a script”Goal: add a continuous slider with two decimals to a panel.
#pragma lifecycle(startup)
// Scene: a UI Panel named 'Physics Panel'. Other scripts listen for 'physics/gravity'.function startup() { const slider = new UISliderEntity(); slider.getComponent(UISliderComponent).setProps({ text: 'Gravity', min: 0, max: 20, step: 0.01, unit: 'm/s²', color: '#38bdf8', }); slider.addComponent(new SignalEmitterComponent({ signal: 'physics:gravity', eventFilter: ['UISlider::change-end'] })); findEntity('Physics Panel').add(slider); slider.setValue(9.81);
on('physics:gravity', (event) => { emit('physics/gravity', { value: event.data.value }); });}How it works:
new UISliderEntity()creates a slider;step: 0.01snaps it to hundredths.- The Signal Emitter’s
eventFilterlets onlyUISlider::change-endthrough, so the listener needs noeventTypecheck. setValue(9.81)on the entity sets the starting value; a script call sendschange, which the filter drops.
Components
Section titled “Components”What a new UI Slider 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 Slider | comp- |
Required | One | Drag to pick a value. |
You can also add 4 optional components, such as Networkable, Networkable Trait, Signal Emitter, 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 |
|---|---|---|
min |
0 |
- |
max |
100 |
- |
step |
1 |
0 ≤ x ≤ 1000000000 |
showValue |
true |
|
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.
UISlider::changeUISlider::change-end
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.
Two UI Sliders drive the delay time and feedback of an Audio Echo Filter on a looping beat; one listener tells the sliders apart by entity id.
With your sound on, click once to start the audio, then drag the sliders: longer times space the repeats out, more feedback makes them last. The beat is a short loop generated for this example. Drag outside the panel 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 Slider.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My UI Slider'); if (!(entity instanceof UISliderEntity)) return;
// Its UI Slider component holds the settings listed under Props. const component = entity.getComponent(UISliderComponent); log('min is', component.props.min); component.setProps({ min: 1 });}