Skip to content

UI Menu

Updated

Opens a popup menu when clicked.

Type
ent-ui-menu
Script class
UIMenuEntity
Family
UI

A UI Menu is a button that opens a popup of choices: File, Options, a profile menu, a “More” button. It is a UI Button (comp-ui-button) with a UI Menu component (comp-ui-menu). Pressing it opens a popup panel beside it, and every UI element placed under the menu entity goes into that popup instead of being laid out next to the button. The entries are usually UI Menu Item entities.

The menu button sits nested in a panel or layout, for example in a toolbar. The popup closes after an entry is chosen, on a press anywhere outside it, and when its scene leaves. A UI Menu on a menu item makes that entry open a submenu.

With a Signal Emitter on the menu entity, it reports UIMenu::open and UIMenu::close. Each entry reports its own choice from its own entity.

Use a UI Menu for a handful of actions or options that need not be visible all the time. For a long, scrolling set of choices, use a UI List. For a single action, a UI Button.

The props that matter most:

  • text and icon on its UI Button: what the menu button shows.
  • trigger: 'select' (the default) opens on a press; 'hover' opens while the pointer rests on it.
  • placement and alignment: the side the popup opens on ('bottom' by default) and its alignment along that side.
  • layout: 'vertical' (the default) or 'horizontal' entries.
  • minWidth, gap and padding: the popup’s minimum width and spacing, in layout pixels (px).

From a script, the component’s open(), close(), toggle(), setOpen(open), isOpen() and getItems() control it; the UI Menu entity has open(), close(), toggle(), isOpen() and items().

  1. Add a UI Menu under a UI Panel or UI Layout. See Scenes and hierarchy.
  2. Set its button text, and placement if the popup should open on another side.
  3. Add UI Menu Item entities directly under the menu, one per entry, and set each one’s text and value.
  4. Give each entry a Signal Emitter (they can share one signal) and listen for EventType.UIMenuItem.Select.

Goal: a Game menu with Resume, Restart and Quit entries.

GameMenu.jx
#pragma lifecycle(startup)
// Scene: a UI Menu named 'Game' with three UI Menu Items whose values are
// 'resume', 'restart' and 'quit', each with a Signal Emitter on 'menu:game'
// whose eventFilter is ['UIMenuItem::select'].
function startup() {
on('menu:game', (event) => {
if (event.eventType !== EventType.UIMenuItem.Select) return;
switch (event.data.value) {
case 'resume':
break;
case 'restart':
emit('level/reset');
break;
case 'quit':
emit('game/quit');
break;
}
});
}

How it works:

  • Each entry sends UIMenuItem::select from its own entity, with its value; one shared signal lets one listener handle every entry.
  • The eventFilter keeps the entries’ button presses off the signal, so each choice is handled once.
  • Choosing any entry closes the popup by itself; “Resume” needs no code.

Goal: stop game time whenever the menu’s popup is open.

PauseOnMenu.jx
#pragma lifecycle(startup)
// Scene: a UI Menu named 'Game' with a Signal Emitter on 'menu:game-state'.
function startup() {
on('menu:game-state', (event) => {
if (event.eventType === EventType.UIMenu.Open) Time.pause();
if (event.eventType === EventType.UIMenu.Close) Time.resume();
});
}

How it works:

  • The menu’s own Signal Emitter reports UIMenu::open and UIMenu::close, however the popup opens or closes.
  • Time.pause() stops game time and Time.resume() carries on, so the world waits while the player chooses.

Goal: during a tutorial, open the menu for the player, opening to the right, and point out its entries.

TutorialMenu.jx
#pragma lifecycle(startup)
// Scene: a UI Menu named 'Tools'. Another script emits 'tutorial:show-tools'.
function startup() {
const tools = findEntity('Tools').getComponent(UIMenuComponent);
tools.setProps({ placement: 'right', alignment: 'start', minWidth: 200 });
on('tutorial:show-tools', () => {
tools.open();
const names = tools.getItems().map((item) => item.value).join(', ');
NotificationService.show(NotificationType.Info, { title: 'Your tools', text: names }, { anchor: 'top' });
});
}

How it works:

  • placement: 'right' opens the popup to the right of the button, and alignment: 'start' aligns it with the start of that side rather than its centre.
  • open() opens the popup without a press; it then closes the usual ways.
  • getItems() returns the entries’ UI Menu Item components, and each value names one entry.

What a new UI Menu 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 comp-ui-menu Required One Opens a popup menu when clicked.

You can also add 4 optional components, such as Link, Signal Emitter, 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"
Prop Default Range
layout "vertical"
trigger "select"
placement "bottom"
open false

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

  • UIButton::press
  • UIMenu::open
  • UIMenu::close

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.

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