UI Checkbox
Updated
A checkbox with a label.
- Type
ent-ui-checkbox- Script class
UICheckboxEntity- Family
- UI
Overview
Section titled “Overview”A UI Checkbox is a small square box with a label beside it that the player ticks on and off. Its UI Checkbox component (comp-ui-checkbox) keeps the state in checked and sends UICheckbox::change with { checked } each time it flips. With radio: true it draws a ring and a dot instead of a box: the classic radio button, for one choice among several. A new checkbox shows the label Checkbox.
Checkboxes sit nested in a panel or layout, usually one per row in a column of options.
Three controls hold an on/off state. Choose by what the player is doing:
- UI Checkbox: an option the player ticks, such as “I have read the rules”, or one item in a list of choices where several can be on (“Cheese”, “Olives”, “Basil”). With
radio: trueand a UI Selection Group, one choice among several. - UI Switch: a setting that takes effect at once, such as music on or off.
- 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 box. An explicitly empty text draws no label.checked: whether it starts ticked.radio: a round ring and dot instead of a square box.size: the width and height of the box in layout pixels (px).color: one colour for the border, the label and the mark.disabled: dims the checkbox and ignores presses.
From a script, isChecked() reads the state, and setChecked(checked) and toggle() change it. Both send UICheckbox::change, exactly as a press does.
Set it up
Section titled “Set it up”- Add a UI Checkbox under a UI Panel or UI Layout, one per option. See Scenes and hierarchy.
- Set its
textand whether it startschecked. - Add a Signal Emitter with a
signal, so it reports each change. Several checkboxes can share one signal. - For radio buttons, set
radio: trueon each and add a UI Selection Group with the samegroupname to each.
Examples
Section titled “Examples”Accept the rules before continuing
Section titled “Accept the rules before continuing”Goal: keep the Continue button disabled until the player ticks the rules checkbox.
#pragma lifecycle(startup)
// Scene: a UI Panel holding a UI Checkbox named 'Accept' (Signal Emitter: 'intro:accept')// and a UI Button named 'Continue' (Signal Emitter: 'intro:continue').function startup() { const accept = findEntity('Accept').getComponent(UICheckboxComponent); const next = findEntity('Continue').getComponent(UIButtonComponent);
accept.setProps({ text: 'I have read the house rules' }); next.setDisabled(!accept.isChecked());
on('intro:accept', (event) => { if (event.eventType === EventType.UICheckbox.Change) next.setDisabled(!event.data.checked); });
on('intro:continue', (event) => { if (event.eventType === EventType.UIButton.Press) emit('intro/done'); });}How it works:
UICheckbox::changecarries the new state inevent.data.checked, and the button is disabled whenever it isfalse.- The button starts in the right state because the script reads
isChecked()once at start. - The Continue listener only runs when the button is enabled, so it never fires before the rules are accepted.
Toppings: several choices at once
Section titled “Toppings: several choices at once”Goal: let the player tick any number of toppings and show the price as they change.
#pragma lifecycle(startup)
// Scene: a UI Panel with UI Checkboxes named 'Cheese', 'Olives' and 'Basil',// each with a Signal Emitter on 'order:topping', and a UI Label named 'Price'.const PRICES = { Cheese: 1.5, Olives: 1, Basil: 0.5 };const BASE = 8;
function startup() { const price = findEntity('Price').getComponent(UILabelComponent); const boxes = Object.keys(PRICES).map((name) => findEntity(name).getComponent(UICheckboxComponent));
function refresh() { let total = BASE; for (const box of boxes) { if (box.isChecked()) total += PRICES[box.props.text]; } price.setProps({ text: `Total: ${total.toFixed(2)} coins` }); }
for (const box of boxes) box.setProps({ text: box.entity.name });
on('order:topping', (event) => { if (event.eventType === EventType.UICheckbox.Change) refresh(); });
refresh();}How it works:
- All three checkboxes share one signal, so one listener reacts to any of them.
refreshdoes not use the event’s data: it reads every checkbox withisChecked()and sums the prices, so the total is always right.box.entityis the entity a component is on, andbox.props.textreads the label each box was given.
Radio buttons for a camera view
Section titled “Radio buttons for a camera view”Goal: three round radio buttons pick one camera view.
#pragma lifecycle(startup)
// Scene: UI Checkboxes named 'First person', 'Third person' and 'Top down',// each with a Signal Emitter on 'settings:view'.const VIEWS = ['First person', 'Third person', 'Top down'];
function startup() { for (const name of VIEWS) { const entity = findEntity(name); entity.getComponent(UICheckboxComponent).setProps({ text: name, radio: true }); entity.addComponent(new UISelectionGroupComponent({ group: 'camera-view' })); } findEntity('Third person').getComponent(UICheckboxComponent).setChecked(true);
on('settings:view', (event) => { if (event.eventType !== EventType.UICheckbox.Change || !event.data.checked) return; emit('camera/view', { view: findEntity(event.entityId).name }); });}How it works:
radio: truedraws each checkbox as a ring and dot. The UI Selection Group with the samegroupmakes them exclusive.setChecked(true)picks the starting view and applies the group, so the other two start unticked.- The listener acts only on
checked: trueand reads which view was picked from the entity that sent the event.
Components
Section titled “Components”What a new UI Checkbox 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 Checkbox | comp- |
Required | One | A checkbox with a label. |
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 |
"Checkbox" |
|
checked |
false |
|
radio |
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.
UICheckbox::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.
Three UI Checkboxes drawn as radio buttons and made exclusive by a UI Selection Group, which a Behavior script adds; the log reports the chosen difficulty.
Pick Easy, Normal or Hard: the others turn off and the log names your choice. Drag outside the panel to orbit the camera, scroll to zoom.
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 Checkbox.
#pragma lifecycle(startup)
function startup() { // Find the entity by the name it has in the Hierarchy. const entity = findEntity('My UI Checkbox'); if (!(entity instanceof UICheckboxEntity)) return;
// Its UI Checkbox component holds the settings listed under Props. const component = entity.getComponent(UICheckboxComponent); log(component.props);}