UI Menu
Updated
Opens a popup menu when clicked.
- Type
ent-ui-menu- Script class
UIMenuEntity- Family
- UI
Overview
Section titled “Overview”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:
textandiconon its UI Button: what the menu button shows.trigger:'select'(the default) opens on a press;'hover'opens while the pointer rests on it.placementandalignment: the side the popup opens on ('bottom'by default) and its alignment along that side.layout:'vertical'(the default) or'horizontal'entries.minWidth,gapandpadding: 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().
Set it up
Section titled “Set it up”- Add a UI Menu under a UI Panel or UI Layout. See Scenes and hierarchy.
- Set its button
text, andplacementif the popup should open on another side. - Add UI Menu Item entities directly under the menu, one per entry, and set each one’s
textandvalue. - Give each entry a Signal Emitter (they can share one signal) and listen for
EventType.UIMenuItem.Select.
Examples
Section titled “Examples”A game menu
Section titled “A game menu”Goal: a Game menu with Resume, Restart and Quit entries.
#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::selectfrom its own entity, with itsvalue; one shared signal lets one listener handle every entry. - The
eventFilterkeeps 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.
Pause the game while the menu is open
Section titled “Pause the game while the menu is open”Goal: stop game time whenever the menu’s popup is open.
#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::openandUIMenu::close, however the popup opens or closes. Time.pause()stops game time andTime.resume()carries on, so the world waits while the player chooses.
Open the menu from a script
Section titled “Open the menu from a script”Goal: during a tutorial, open the menu for the player, opening to the right, and point out its entries.
#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, andalignment: '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 eachvaluenames one entry.
Components
Section titled “Components”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- |
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 | 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({ … }).
UI Button
Section titled “UI Button”| Prop | Default | Range |
|---|---|---|
text |
"Button" |
|
iconPosition |
"left" |
|
align |
"center" |
|
disabled |
false |
|
variant |
"solid" |
UI Menu
Section titled “UI Menu”| Prop | Default | Range |
|---|---|---|
layout |
"vertical" |
|
trigger |
"select" |
|
placement |
"bottom" |
|
open |
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.
UIButton::pressUIMenu::openUIMenu::close
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.
#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);}