Skip to content

UI Button

Updated

A button with a label or an icon; each press sends an event.

Type
ent-ui-button
Script class
UIButtonEntity
Family
UI

A UI Button is something to press: Play, Buy, Close, Next. Its UI Button component (comp-ui-button) draws a box with a text label, an icon, or both, which lights up on hover and while held. A press counts when the pointer is released over the same button, whether that pointer is a mouse, a finger or an XR controller or hand ray; releasing elsewhere cancels it.

A button is usually nested in a panel or layout. It can also stand alone as a root, in world space at its transform, for a single button floating by a door or a machine. A new button shows the text Button.

A button tells scripts it was pressed only while its entity has a Signal Emitter: each press then sends UIButton::press on the emitter’s signal. With a Link component, a press opens a web page instead and sends no press event.

Use a UI Button for an action that happens once per press. For something that stays on or off, use a UI Toggle Button (a button that latches), a UI Switch or a UI Checkbox. For a button that opens a popup of choices, use a UI Menu.

The props that matter most:

  • text and icon: the label and an icon name, shown together or alone; iconPosition puts the icon 'left' (the default), 'right', 'top' or 'bottom' of the label.
  • variant: 'solid' (the default) is filled; 'subtle' shows a fill only on hover; 'bare' is the content alone.
  • backgroundColor and color: the fill of a solid button and the colour of its label and icon.
  • disabled: dims the button and ignores presses.
  • badge: a count shown in a small badge at the top right; 0 hides it.

From a script, the component’s setText(text), setBadge(count), setDisabled(disabled) and press() change it without touching the other props.

  1. Add a UI Button directly under a UI Panel or UI Layout. See Scenes and hierarchy.
  2. Set its text, and an icon if you want one.
  3. Add a Signal Emitter to the button’s own entity and set its signal, for example door:open. Without it, the button sends nothing.
  4. In a Behavior, listen with on('door:open', …) and check event.eventType === EventType.UIButton.Press.

Goal: a button opens a door, then disables itself so it cannot be pressed twice.

OpenDoor.jx
#pragma lifecycle(startup)
// Scene: a UI Button named 'Open Door' with a Signal Emitter on 'door:open'.
// A script on the door listens for the signal 'door/opening'.
function startup() {
const button = findEntity('Open Door').getComponent(UIButtonComponent);
on('door:open', (event) => {
if (event.eventType !== EventType.UIButton.Press) return;
button.setText('Opening…');
button.setDisabled(true);
emit('door/opening');
});
}

How it works:

  • on('door:open', …) runs each time the button’s Signal Emitter sends an event on that signal; the eventType check ignores anything else that shares it.
  • setText changes only the label; setDisabled(true) dims the button and stops further presses, so the listener cannot run again.
  • emit('door/opening') hands the work to whichever script moves the door.

Goal: fill a panel with one button per destination, each wired to its own action.

Destinations.jx
#pragma lifecycle(startup)
// Scene: a UI Panel named 'Travel'. Spawn points elsewhere listen for 'travel/to'.
const PLACES = ['Lobby', 'Gallery', 'Rooftop'];
function startup() {
const panel = findEntity('Travel');
for (const place of PLACES) {
const button = new UIButtonEntity();
button.getComponent(UIButtonComponent).setProps({ text: place, variant: 'subtle', align: 'left' });
button.addComponent(new SignalEmitterComponent({ signal: 'travel:pick' }));
button.name = place;
panel.add(button);
}
on('travel:pick', (event) => {
if (event.eventType !== EventType.UIButton.Press) return;
const place = findEntity(event.entityId).name;
emit('travel/to', { place });
});
}

How it works:

  • new UIButtonEntity() creates a button; setProps sets its text, a subtle look and left-aligned content.
  • Every button gets a Signal Emitter on the same signal, travel:pick, so one listener handles them all.
  • event.entityId is the id of the button that was pressed; findEntity returns it, and its name says which place it stands for.

Goal: show how many items are in the cart on the button’s badge, and keep it disabled while the cart is empty.

CartButton.jx
#pragma lifecycle(startup)
// Scene: a UI Button named 'Cart' with a Signal Emitter on 'shop:cart'.
// Other scripts emit 'cart/add' when the player picks up an item.
let items = 0;
function startup() {
const cart = findEntity('Cart').getComponent(UIButtonComponent);
function refresh() {
cart.setBadge(items);
cart.setDisabled(items === 0);
cart.setText(items === 0 ? 'Cart is empty' : 'Check out');
}
on('cart/add', () => {
items += 1;
refresh();
});
on('shop:cart', (event) => {
if (event.eventType !== EventType.UIButton.Press) return;
emit('shop/checkout', { items });
items = 0;
refresh();
});
refresh();
}

How it works:

  • setBadge(items) shows the count in the badge; a count of 0 hides it.
  • The button stays disabled while the cart is empty, so the press listener only runs when there is something to check out.
  • refresh redraws the button from the items variable after every change.

What a new UI Button 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 Button comp-ui-button Required One A button with a label or an icon; each press sends an event.

You can also add 5 optional components, such as Link, Signal Emitter, UI Menu, UI Menu Item, 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 "Button"
iconPosition "left"
align "center"
disabled false
variant "solid"

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

  • UIButton::press

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

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