Skip to content

UI Slider

Updated

Drag to pick a value.

Type
ent-ui-slider
Script class
UISliderEntity
Family
UI

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 calls setValue. 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, max and step: the range (0 to 100 by default) and the step the value snaps to (1 by default; 0 for a continuous value).
  • value: the current value, kept within the range.
  • text, showValue and unit: 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.

  1. Add a UI Slider under a UI Panel or UI Layout. See Scenes and hierarchy.
  2. Set text, min, max, step and a starting value; add a unit if it helps.
  3. Add a Signal Emitter with a signal, so it reports changes.
  4. In a Behavior, listen with on(signal, …); handle EventType.UISlider.Change for live updates and EventType.UISlider.ChangeEnd for once-per-drag work.

Goal: drag a slider to change a point light’s brightness as the thumb moves.

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

  • setProps sets the range and a step of 5, and unit: '%' 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’s setValue sends change just as a drag does, so the light takes the matching intensity from the start.

Goal: show the field of view while dragging, and announce it once when the player lets go.

FieldOfView.jx
#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 eventType to tell them apart.
  • change updates the note on every step of the drag.
  • change-end arrives once, when the drag is released with a new value, so the setting is announced once rather than dozens of times per drag.

Goal: add a continuous slider with two decimals to a panel.

Gravity.jx
#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.01 snaps it to hundredths.
  • The Signal Emitter’s eventFilter lets only UISlider::change-end through, so the listener needs no eventType check.
  • setValue(9.81) on the entity sets the starting value; a script call sends change, which the filter drops.

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-transform Required One Position, rotation and scale of the entity, with optional editor-only per-axis locks.
UI Slider comp-ui-slider 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 -1000000000 ≤ x ≤ 1000000000
max 100 -1000000000 ≤ x ≤ 1000000000
step 1 0 ≤ x ≤ 1000000000
showValue true
disabled false

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

  • UISlider::change
  • UISlider::change-end

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 Slider.

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