UI Menu Item
Updated
One entry of a UI Menu.
- Type
ent-ui-menu-item- Script class
UIMenuItemEntity- Family
- UI
Overview
Section titled “Overview”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:
valueon the UI Menu Item: the identifier your script receives.text,iconandvarianton its UI Button: what the entry shows.disabledon its UI Button: a dimmed entry that cannot be chosen.badgeon its UI Button: a count beside the entry.
Set it up
Section titled “Set it up”- Add UI Menu Item entities directly under a UI Menu. See Scenes and hierarchy.
- Set each one’s button
textand itsvalue. - Add a Signal Emitter to each entry; one signal for all entries of a menu is simplest. Set its
eventFilterto['UIMenuItem::select']to keep the button presses off the signal. - For a submenu, add a UI Menu component to an entry and put the submenu’s entries under it.
Examples
Section titled “Examples”Handle every entry on one signal
Section titled “Handle every entry on one signal”Goal: a View menu whose entries switch the camera mode.
#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.entityis each entry’s entity.- Each entry gets a Signal Emitter on one shared signal, filtered to
UIMenuItem::select, so the listener needs noeventTypecheck. event.data.valueis the chosen entry’svalue.
Disable an entry until it makes sense
Section titled “Disable an entry until it makes sense”Goal: “Save” is disabled until something has changed, and shows a badge with the number of changes.
#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
setDisabledandsetBadgecome fromUIButtonComponent. - A disabled entry cannot be chosen, so the save listener only runs when there is something to save.
Components
Section titled “Components”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- |
Required | One | Position, rotation and scale of the entity, with optional editor-only per-axis locks. |
| UI Button | comp- |
Required | One | A button with a label or an icon; each press sends an event. |
| UI Menu Item | comp- |
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" |
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.
UIButton::pressUIMenuItem::select
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.
A File menu with three UI Menu Items, a tooltip added by a script, and an in-scene QWERTY UI Keyboard whose key presses play a click.
Rest the pointer on File to see its tooltip, press it and choose an entry: the log names it. Click the note field and press keys on the in-scene keyboard to hear them click. 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 Menu Item.
#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);}