Elements
ContextMenu
A floating context menu element that renders in a portal overlay. Used for dropdown menus, right-click menus, and action menus. Contains MenuItem and MenuDivider children.
Constructor
new ContextMenu(
new MenuItem(new Label("Edit")).OnClick(_ => Edit()),
new MenuItem(new Label("Duplicate")).OnClick(_ => Duplicate()),
new MenuDivider(),
new MenuItem(new Label("Delete")).OnClick(_ => Delete())
);Examples
Dropdown Menu (Left-Click)
Attach a dropdown menu to a button that opens on click:
var optionsMenu = new ContextMenu(
new MenuItem(new Label("Option 1")).OnClick(_ => SelectOption(1)),
new MenuItem(new Label("Option 2")).OnClick(_ => SelectOption(2)),
new MenuItem(new Label("Option 3")).OnClick(_ => SelectOption(3))
);
new Button(new Label("Options"))
.WithContextMenu(optionsMenu);Right-Click Context Menu
Attach a context menu that opens at cursor position on right-click:
var contextMenu = new ContextMenu(
new MenuItem(new Label("Cut")).OnClick(_ => Cut()),
new MenuItem(new Label("Copy")).OnClick(_ => Copy()),
new MenuItem(new Label("Paste")).OnClick(_ => Paste())
);
new Label("Right-click me")
.WithRightClickMenu(contextMenu);Menu with Icons
Menu items can include icons from Resources:
new ContextMenu(
new MenuItem(new Image { image = editIcon }, new Label("Edit")).OnClick(_ => Edit()),
new MenuItem(new Image { image = copyIcon }, new Label("Duplicate")).OnClick(_ => Duplicate()),
new MenuDivider(),
new MenuItem(new Image { image = trashIcon }, new Label("Delete")).OnClick(_ => Delete())
);Scene Object Context Menu
Open a context menu from game code (e.g., after raycasting to a scene object):
// In your MonoBehaviour
void Update()
{
if (Input.GetMouseButtonDown(1)) // Right click
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out var hit))
{
// Configure menu based on clicked object
ConfigureMenuForObject(hit.collider.gameObject);
// Open at cursor position with proper coordinate conversion
myContextMenu.OpenAtCursor();
}
}
}Dynamic Menu Items
Use BindChildren to create menu items from reactive data:
var recentFiles = new ReactiveProperty<string[]>(new[] { "file1.txt", "file2.txt" });
new ContextMenu()
.BindChildren(recentFiles, fileName =>
new MenuItem(new Label(fileName)).OnClick(_ => OpenFile(fileName))
);Programmatic Open/Close
Control menu state programmatically:
var menu = new ContextMenu(
new MenuItem(new Label("Action")).OnClick(_ => DoAction())
);
// Open at specific screen position
menu.OpenAtScreenPosition(new Vector2(100, 200));
// Open at current cursor
menu.OpenAtCursor();
// Close the menu
menu.Close();
// Bind to reactive state
menu.BindOpen(ShowMenu);Nested with Callbacks
Register callbacks for when the menu closes:
new ContextMenu(
new MenuItem(new Label("Save")).OnClick(_ => Save()),
new MenuItem(new Label("Cancel")).OnClick(_ => Cancel())
)
.OnClose(() => Debug.Log("Menu closed"));Properties
| Prop | Type | Default |
|---|---|---|
children? | IElement[] | [] |
open? | bool | false |
disabled? | bool | false |
visible? | bool | true |
name? | string | "" |
Methods
| Prop | Type | Default |
|---|---|---|
Open? | (bool = true) => ContextMenu | - |
Close? | () => ContextMenu | - |
GetOpen? | () => bool | - |
BindOpen? | (Observable<bool>) => ContextMenu | - |
OnClose? | (Action) => ContextMenu | - |
Position? | (float x, float y) => ContextMenu or (Vector2) => ContextMenu | - |
OpenAtScreenPosition? | (Vector2 screenPosition) => ContextMenu | - |
OpenAtCursor? | () => ContextMenu | - |
AnchorTo? | (VisualElement, AnchorPosition = Below) => ContextMenu | - |
BindChildren? | <TChild>(Observable<TChild[]>, Func<TChild, IElement>) => ContextMenu | - |
Extension Methods
See Popover Extension Methods. These accept any Popover (including ContextMenu).
AnchorPosition Enum
| Prop | Type | Default |
|---|---|---|
Below? | AnchorPosition | - |
Above? | AnchorPosition | - |
Left? | AnchorPosition | - |
Right? | AnchorPosition | - |
CSS Classes
.elements-context-menu- The menu container (visual styles).elements-popover- Inherited popover base (positioning, open/close animation).elements-popover.open- Applied when menu is open.elements-popover-backdrop- Invisible backdrop for click-outside detection
ContextMenu extends Popover which extends Group which extends BaseElement.