This guide walks you through setting up the UI Service in your Unity project and creating your first UI presenter.
- Unity 6000.0 or higher
- Addressables 2.6.0 or higher
- UniTask 2.5.10 or higher
- Open Unity Package Manager (
Window→Package Manager) - Click the
+button and selectAdd package from git URL - Enter the following URL:
https://github.com/CoderGamester/com.gamelovers.uiservice.git
Add the following line to your project's Packages/manifest.json:
{
"dependencies": {
"com.gamelovers.uiservice": "https://github.com/CoderGamester/com.gamelovers.uiservice.git"
}
}openupm add com.gamelovers.uiserviceThe UI Service requires a configuration asset to know about your UI presenters.
- Right-click in Project View
- Navigate to
Create→ScriptableObjects→Configs→UiConfigs - Name it (e.g.,
GameUiConfigs)
This ScriptableObject will store:
- UI presenter types and their addressable addresses
- Layer assignments for depth sorting
- UI set groupings for batch operations
Create a game initializer script to set up the UI Service:
using UnityEngine;
using GameLovers.UiService;
public class GameInitializer : MonoBehaviour
{
[SerializeField] private UiConfigs _uiConfigs;
private IUiServiceInit _uiService;
void Start()
{
// Create and initialize the UI service
_uiService = new UiService();
_uiService.Init(_uiConfigs);
// The service is now ready to use
}
void OnDestroy()
{
// Clean up when done
_uiService?.Dispose();
}
}The UI Service supports multiple asset loading strategies out of the box:
| Loader | Scenario |
|---|---|
AddressablesUiAssetLoader |
Recommended - Uses Unity's Addressables system for async loading. |
PrefabRegistryUiAssetLoader |
Best for samples or when prefabs are directly referenced in game code. |
ResourcesUiAssetLoader |
Loads assets from the Resources folder (traditional Unity workflow). |
Example using the ResourcesUiAssetLoader:
void Start()
{
// Initialize using Resources loader
_uiService = new UiService(new ResourcesUiAssetLoader());
_uiService.Init(_uiConfigs);
}using UnityEngine;
using UnityEngine.UI;
using GameLovers.UiService;
public class MainMenuPresenter : UiPresenter
{
[SerializeField] private Button _playButton;
[SerializeField] private Button _settingsButton;
protected override void OnInitialized()
{
// Called once when the presenter is first loaded
_playButton.onClick.AddListener(OnPlayClicked);
_settingsButton.onClick.AddListener(OnSettingsClicked);
}
protected override void OnOpened()
{
// Called every time the UI is shown
Debug.Log("Main menu opened!");
}
protected override void OnClosed()
{
// Called when the UI is hidden
Debug.Log("Main menu closed!");
}
private void OnPlayClicked()
{
Close(destroy: false);
// Open gameplay UI...
}
private void OnSettingsClicked()
{
// Open settings...
}
}- Create a new Canvas in your scene
- Add your UI elements (buttons, text, images)
- Add the
MainMenuPresenterscript to the Canvas - Important: Set the prefab's root GameObject to disabled (the service will enable it when opened)
- Save as a prefab
- Select your prefab in the Project view
- In the Inspector, check "Addressable"
- Set the address (e.g.,
UI/MainMenu)
- Open your
UiConfigsasset - Add a new entry:
- Type:
MainMenuPresenter - Address:
UI/MainMenu - Layer:
1(or your preferred layer)
- Type:
public class GameManager : MonoBehaviour
{
private IUiService _uiService;
async void Start()
{
// Open main menu
var mainMenu = await _uiService.OpenUiAsync<MainMenuPresenter>();
// Check if a UI is visible
if (_uiService.IsVisible<MainMenuPresenter>())
{
Debug.Log("Main menu is currently visible");
}
// Close specific UI (keeps in memory for fast reopen)
_uiService.CloseUi<MainMenuPresenter>();
// Close and destroy (frees memory)
_uiService.CloseUi<MainMenuPresenter>(destroy: true);
// Close all visible UI
_uiService.CloseAllUi();
}
}For UI that needs initialization data:
// Define your data structure
public struct PlayerProfileData
{
public string PlayerName;
public int Level;
public Sprite Avatar;
}
// Create a presenter that uses the data
public class PlayerProfilePresenter : UiPresenter<PlayerProfileData>
{
[SerializeField] private Text _nameText;
[SerializeField] private Text _levelText;
[SerializeField] private Image _avatarImage;
protected override void OnSetData()
{
// Called when data is set - use Data property
_nameText.text = Data.PlayerName;
_levelText.text = $"Level {Data.Level}";
_avatarImage.sprite = Data.Avatar;
}
}
// Open with data
var profileData = new PlayerProfileData
{
PlayerName = "Hero",
Level = 42,
Avatar = avatarSprite
};
await _uiService.OpenUiAsync<PlayerProfilePresenter, PlayerProfileData>(profileData);- Core Concepts - Learn about layers, sets, and features
- API Reference - Complete API documentation
- Advanced Topics - Performance optimization
The package includes sample implementations in the Samples~ folder:
- Open Unity Package Manager (
Window→Package Manager) - Select "UI Service" package
- Navigate to the "Samples" tab
- Click "Import" next to the sample you want
Available samples:
- BasicUiFlow - Basic presenter lifecycle
- DataPresenter - Data-driven UI
- DelayedPresenter - Time and animation delays
- UiToolkit - UI Toolkit integration