Skip to content

UI Menu Item

Updated

One entry of a UI Menu.

Type
ent-ui-menu-item
Script class
UIMenuItemEntity
Family
UI

A UI Menu Item is one entry in the popup of a UI Menu. It is a UI Button (comp-ui-button) with a UI Menu Item component (comp-ui-menu-item): the button gives the entry its look (text, icon, badge, disabled state), and the menu item component marks it as an entry of the nearest UI Menu above it. A new entry shows the text Button.

Place it nested directly under a UI Menu, in the order the entries should appear. Choosing it closes the popup.

Each entry has a value, the identifier reported when it is chosen; it defaults to the entity’s name. With a Signal Emitter on the entry, it reports UIMenuItem::select { value }. Its button also sends its own UIButton::press, so listen for one or the other. An entry that opens a submenu, because it also carries a UI Menu, sends nothing when pressed.

The props that matter most:

  • value on the UI Menu Item: the identifier your script receives.
  • text, icon and variant on its UI Button: what the entry shows.
  • disabled on its UI Button: a dimmed entry that cannot be chosen.
  • badge on its UI Button: a count beside the entry.
  1. Add UI Menu Item entities directly under a UI Menu. See Scenes and hierarchy.
  2. Set each one’s button text and its value.
  3. Add a Signal Emitter to each entry; one signal for all entries of a menu is simplest. Set its eventFilter to ['UIMenuItem::select'] to keep the button presses off the signal.
  4. For a submenu, add a UI Menu component to an entry and put the submenu’s entries under it.

Goal: a View menu whose entries switch the camera mode.

ViewMenu.jx
#pragma lifecycle(startup)
// Scene: a UI Menu named 'View' with UI Menu Items whose values are
// 'first-person', 'third-person' and 'overview'. The script wires them itself.
function startup() {
const view = findEntity('View').getComponent(UIMenuComponent);
for (const item of view.getItems()) {
item.entity.addComponent(new SignalEmitterComponent({ signal: 'menu:view', eventFilter: ['UIMenuItem::select'] }));
}
on('menu:view', (event) => {
emit('camera/mode', { mode: event.data.value });
});
}

How it works:

  • getItems() returns the menu’s entries as UI Menu Item components; item.entity is each entry’s entity.
  • Each entry gets a Signal Emitter on one shared signal, filtered to UIMenuItem::select, so the listener needs no eventType check.
  • event.data.value is the chosen entry’s value.

Goal: “Save” is disabled until something has changed, and shows a badge with the number of changes.

SaveEntry.jx
#pragma lifecycle(startup)
// Scene: a UI Menu named 'File' with a UI Menu Item named 'Save' (value 'save',
// Signal Emitter on 'menu:file' filtered to 'UIMenuItem::select').
// Other scripts emit 'doc/changed' after each edit.
let changes = 0;
function startup() {
const save = findEntity('Save').getComponent(UIButtonComponent);
function refresh() {
save.setDisabled(changes === 0);
save.setBadge(changes);
}
on('doc/changed', () => {
changes += 1;
refresh();
});
on('menu:file', (event) => {
if (event.data.value !== 'save') return;
emit('doc/save');
changes = 0;
refresh();
});
refresh();
}

How it works:

  • A menu item’s look lives on its UI Button, so setDisabled and setBadge come from UIButtonComponent.
  • A disabled entry cannot be chosen, so the save listener only runs when there is something to save.

What a new UI Menu Item 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.
UI Menu Item comp-ui-menu-item Required One One entry of a UI Menu.

You can also add 4 optional components, such as Link, Signal Emitter, UI Menu, 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
  • UIMenuItem::select

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 Menu Item.

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