MenuUIState
The MenuUIState is a flexible, reusable UI-focused game state used to display a menu or user interface screen. It supports both Addressable and non-addressable UI workflows and can preload UI assets to reduce latency during transitions.
🧩 Overview
public class MenuUIState : GameBaseState
Extends the core game state system and is focused on presenting UI to the player.
🎛 Public Configuration
_uiPrefab
Type: AssetReferenceGameObject
UI prefab to instantiate when entering this state (if using Addressables).
requireUIPrewarm
Type: bool
If true, this state can only be entered after UI assets are preloaded via PrewarmUI().
useAddressableAsset
Type: bool
If true, UI is loaded using Unity Addressables. If false, it expects a scene-attached UI GameObject.
🧠 State Lifecycle
EnterState()
public override void EnterState()
- Sets
Application.targetFrameRate = 30. - Calls
LoadAndSpawnUI()to load the UI, depending on whether Addressables are used and if UI was prewarmed.
❌ Exit Handling
ExitState(bool allowRejoin = false)
public override Task ExitState(bool allowRejoin = false)
- Releases addressable assets or disables the UI gameobject depending on the config.
- Cleans up both instanced and preloaded references.
✅ State Transition Conditions
CanEnterState()
public override bool CanEnterState()
- Ensures UI is prewarmed if
requireUIPrewarmistrue.
CanExitState()
public override bool CanExitState()
- Always returns
true.
⚡ Preloading Support
PrewarmUI()
public async Task<bool> PrewarmUI()
- Loads the UI prefab asset into memory.
- Sets
_isUIPreloaded = trueif loading succeeds, making the state enterable ifrequireUIPrewarmis enabled.
🧱 UI Loading Logic
LoadAndSpawnUI()
private async void LoadAndSpawnUI()
- If
useAddressableAssetisfalse: just activates_uiInstance. - If prewarmed: instantiates the preloaded prefab.
- Otherwise: performs an
InstantiateAsync()from Addressables.
🧠 Summary
MenuUIState is a highly adaptable state designed for menu screens. It balances performance and flexibility with preload and runtime loading options. Suitable for splash screens, settings, or other in-game menus in projects utilizing Unity's Addressables system.