Class UnityMenuService
Unity MonoBehaviour implementation of IMenuService that manages menu view display and navigation.
Handles the lifecycle and state management of Unity-based menu views throughout the application.
Namespace: Serenity.Menu.Infrastructure.Services
Assembly: Serenity.UnityMenu.Infrastructure.dll
Syntax
public class UnityMenuService : MonoBehaviour, IMenuService, IService, IFoundationSettings, IViewProvider, IMenuInstanceGate
Constructors
UnityMenuService()
Declaration
public UnityMenuService()
Fields
_gameModeService
Declaration
protected IGameModeService _gameModeService
Field Value
| Type | Description |
|---|---|
| IGameModeService | The GameMode service used to coordinate the application mode when the initial menu is shown. |
_guid
Declaration
protected string _guid
Field Value
| Type | Description |
|---|---|
| string | The globally unique identifier for this menu service instance. |
_id
Declaration
protected string _id
Field Value
| Type | Description |
|---|---|
| string | The unique identifier for this menu service instance. |
_initMenuId
Declaration
protected string _initMenuId
Field Value
| Type | Description |
|---|---|
| string | The ID of the initial menu to show when ShowInitialMenu is called. |
_lazyContext
Declaration
protected UnityMenuLazyBuildContext _lazyContext
Field Value
| Type | Description |
|---|---|
| UnityMenuLazyBuildContext | Shared build inputs reused to construct lazy menu views on first show. |
_lazyRegistrations
Declaration
protected Dictionary<string, UnityMenuLazyRegistration> _lazyRegistrations
Field Value
| Type | Description |
|---|---|
| Dictionary<string, UnityMenuLazyRegistration> | Registrations for LazyOnFirstOpen menus that have not been built yet, keyed by menu id. |
_logService
Declaration
protected ILogService _logService
Field Value
| Type | Description |
|---|---|
| ILogService | ILogService for logging within the menu service. |
_menuSettingsById
Declaration
protected Dictionary<string, UnityMenuSettingsDefinition> _menuSettingsById
Field Value
| Type | Description |
|---|---|
| Dictionary<string, UnityMenuSettingsDefinition> | Menu settings definitions keyed by definition id, used to build owned hosted instances on demand. |
_menuViewModelFactory
Declaration
protected IMenuViewModelFactory _menuViewModelFactory
Field Value
| Type | Description |
|---|---|
| IMenuViewModelFactory | Factory used to build a fresh view model per owned hosted menu instance. |
_menuViews
Declaration
protected Dictionary<string, UnityMenuView> _menuViews
Field Value
| Type | Description |
|---|---|
| Dictionary<string, UnityMenuView> | Dictionary of built Unity menu views keyed by their identifiers for efficient lookup. |
_viewBrowserService
Declaration
protected IViewBrowserService _viewBrowserService
Field Value
| Type | Description |
|---|---|
| IViewBrowserService | Gets or sets the unique identifier for this service. |
_viewModelsRegistry
Declaration
protected Dictionary<string, IViewModel> _viewModelsRegistry
Field Value
| Type | Description |
|---|---|
| Dictionary<string, IViewModel> | The presenter's view-model registry (same reference), mutated so owned hosted instances are routable. |
Properties
Guid
Declaration
public string Guid { get; set; }
Property Value
| Type | Description |
|---|---|
| string | Gets or sets the globally unique identifier for this service. |
Id
Declaration
public string Id { get; set; }
Property Value
| Type | Description |
|---|---|
| string |
LogService
Declaration
public ILogService LogService { get; set; }
Property Value
| Type | Description |
|---|---|
| ILogService |
Methods
CreateHostedInstance(string, string)
Builds and registers an OWNED menu instance for hosting inside a composite view. Unlike GetView(string) (which returns the single shared standalone instance), this creates a brand-new UnityMenuView so the same menu definition can render as several independent instances at once.
Declaration
public IViewBase CreateHostedInstance(string definitionId, string routingId)
Parameters
| Type | Name | Description |
|---|---|---|
| string | definitionId | The menu definition id to instantiate. |
| string | routingId | The globally-unique routing id (composite host path + definition id). |
Returns
| Type | Description |
|---|---|
| IViewBase | The owned, hidden hosted instance, or null when the definition id is unknown. |
Remarks
The new view keeps the definition id on IViewBase.Id (for composite child
addressing via FindChild), while its view model carries routingId so
the presenter routes this instance's show/hide separately. The view model is registered in the
presenter registry and the view in the menu-view registry under the routing id, so the
controller's GetCurrentView() can route input to a shown instance. Game settings stay
shared (keyed by option id) by design — two views of the same setting reflect the same value.
The view model is registered BEFORE the view is built, because the build sequence ends by hiding
the menu (which routes through the presenter).
DeactivateMenuContext()
Declaration
public virtual void DeactivateMenuContext()
EnsureMenu(string)
Ensures the requested menu has a built, cached view, building a LazyOnFirstOpen menu on first request.
Idempotent: an already-built menu returns true without rebuilding; a registered lazy menu is built
once and cached; an unknown menu id fails loudly (throws). A lazy build failure is logged in detail,
then rethrown in editor/development builds (fail-loud) or reported as false in production builds.
Declaration
public virtual bool EnsureMenu(string menuId)
Parameters
| Type | Name | Description |
|---|---|---|
| string | menuId | The id of the menu whose visual instance must exist. |
Returns
| Type | Description |
|---|---|
| bool | True if the menu's view is available after the call; false on a non-fatal (production) build failure. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the menu id is unknown, or when a lazy build fails in editor/development builds. |
GetCurrentView()
Gets the currently active menu view by checking which view's options wrapper is active.
Returns null if no menu view is currently active.
Declaration
public virtual IViewBase GetCurrentView()
Returns
| Type | Description |
|---|---|
| IViewBase | The currently active IViewBase implementation, or null if none are active. |
GetView(string)
Resolves a menu view by id, building a lazy menu if needed, or returns null when no menu owns the id. Used to host menus inside composite views.
Declaration
public virtual IViewBase GetView(string menuId)
Parameters
| Type | Name | Description |
|---|---|---|
| string | menuId | The menu id to resolve. |
Returns
| Type | Description |
|---|---|
| IViewBase | The built menu view, or null when unknown / on a non-fatal build failure. |
HasView(string)
True when this service owns a menu with the given id (built or registered lazy). Non-throwing and side-effect-free — it does not build a lazy menu. Part of IViewProvider.
Declaration
public virtual bool HasView(string viewId)
Parameters
| Type | Name | Description |
|---|---|---|
| string | viewId | The menu id to test. |
Returns
| Type | Description |
|---|---|
| bool | True if a menu with that id exists. |
HideView(string)
Hides a built menu by id (no build, no-op if unknown); IViewProvider entry point.
Declaration
public virtual void HideView(string viewId)
Parameters
| Type | Name | Description |
|---|---|---|
| string | viewId | The menu id to hide. |
InitializeService(Dictionary<string, IViewBase>, IViewBrowserService, string)
Initializes the menu service with the provided menu views.
Validates that all provided views are UnityMenuView instances and stores them for management.
Declaration
public virtual void InitializeService(Dictionary<string, IViewBase> menuViews, IViewBrowserService viewBrowserService, string initMenuId)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, IViewBase> | menuViews | Dictionary of menu views keyed by their identifiers. |
| IViewBrowserService | viewBrowserService | Service for managing view navigation and lifecycle operations between menu screens. |
| string | initMenuId | The ID of the initial menu to show when ShowInitialMenu is called. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when a view is not a UnityMenuView instance. |
RegisterHostingDependencies(Dictionary<string, IViewModel>, IMenuViewModelFactory, Dictionary<string, UnityMenuSettingsDefinition>)
Wires the dependencies needed to build OWNED hosted menu instances on demand (see CreateHostedInstance(string, string)): the presenter's view-model registry (the same dictionary reference the presenter holds, so newly built instances become routable), the view-model factory, and the menu-settings lookup by definition id. Called by the installer after RegisterLazyMenus(Dictionary<string, UnityMenuLazyRegistration>, UnityMenuLazyBuildContext).
Declaration
public void RegisterHostingDependencies(Dictionary<string, IViewModel> viewModelRegistry, IMenuViewModelFactory menuViewModelFactory, Dictionary<string, UnityMenuSettingsDefinition> menuSettingsById)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, IViewModel> | viewModelRegistry | The presenter's view-model dictionary (same reference). |
| IMenuViewModelFactory | menuViewModelFactory | Factory that builds a fresh view model from a menu definition. |
| Dictionary<string, UnityMenuSettingsDefinition> | menuSettingsById | All menu settings definitions keyed by definition id. |
RegisterLazyMenus(Dictionary<string, UnityMenuLazyRegistration>, UnityMenuLazyBuildContext)
Registers LazyOnFirstOpen menus and the shared build context used to construct their views on first show.
Intended to be called by the installer immediately after InitializeService(Dictionary<string, IViewBase>, IViewBrowserService, string). Safe to call
with an empty registration set (the all-PreloadOnStartup default), in which case no menu is ever lazy.
Declaration
public void RegisterLazyMenus(Dictionary<string, UnityMenuLazyRegistration> lazyRegistrations, UnityMenuLazyBuildContext context)
Parameters
| Type | Name | Description |
|---|---|---|
| Dictionary<string, UnityMenuLazyRegistration> | lazyRegistrations | Per-menu lazy registrations keyed by menu id. |
| UnityMenuLazyBuildContext | context | Shared build inputs reused to construct any lazy menu view. |
SetGameModeService(IGameModeService)
Sets the GameMode service used to coordinate the application mode when the initial menu is shown.
Intended to be invoked by the composition root once the GameMode service has been constructed.
Declaration
public virtual void SetGameModeService(IGameModeService gameModeService)
Parameters
| Type | Name | Description |
|---|---|---|
| IGameModeService | gameModeService | The GameMode service implementation to coordinate with. |
ShowInitialMenu()
Shows the initial menu configured during initialization.
This is the menu that should be displayed at startup or when returning from gameplay.
Declaration
public virtual void ShowInitialMenu()
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when InitMenuId is not configured or the menu is not found. |
ShowMenu(string)
Shows the menu with the specified identifier by executing its show menu operation.
Activates the requested menu view and makes it visible to the user.
Declaration
public virtual void ShowMenu(string menuId)
Parameters
| Type | Name | Description |
|---|---|---|
| string | menuId | The identifier of the menu to show. |
Exceptions
| Type | Condition |
|---|---|
| Exception | Thrown when the specified menu ID is not found in the service. |
ShowView(string)
Shows a menu by id; the IViewProvider entry point used by the view router.
Declaration
public virtual void ShowView(string viewId)
Parameters
| Type | Name | Description |
|---|---|---|
| string | viewId | The menu id to show. |