A powerful and flexible UI management system for Unity that provides a robust abstraction layer for handling game UI with support for layers, async loading, and UI sets. This service streamlines UI development by managing the complete lifecycle of UI presenters, from loading and initialization to display and cleanup.
- Key Features
- System Requirements
- Installation
- Quick Start
- Core Concepts
- API Documentation
- Advanced Features
- Package Structure
- Dependencies
- Migration Guide
- Contributing
- Support
- License
- 🎭 UI Presenter Pattern - Clean separation of UI logic with lifecycle management
- 📚 Layer-based Organization - Organize UI elements by depth layers
- 🔄 Async Loading - Load UI assets asynchronously with UniTask support
- 📦 UI Sets - Group related UI elements for batch operations
- 💾 Memory Management - Efficient loading/unloading of UI assets
- 🎯 Type-safe API - Generic methods for compile-time safety
- 📱 Responsive Design - Built-in support for safe areas and screen size adjustments
- 🔧 Addressables Integration - Seamless integration with Unity's Addressables system
- 🎨 UI Toolkit Support - Compatible with both uGUI and UI Toolkit
- Unity 6000.0 or higher
- Addressables 1.22.0 or higher
- UniTask 2.5.10 or higher
- TextMeshPro 3.0.9 or higher
- Git (for installation via Package Manager)
- 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"
}
}
First, create a UI configuration asset:
- Right-click in Project View
- Navigate to
Create
→ScriptableObjects
→Configs
→UiConfigs
- Configure your UI presenters in the created asset
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);
}
}
using UnityEngine;
using GameLovers.UiService;
public class MainMenuPresenter : UiPresenter
{
[SerializeField] private Button _playButton;
[SerializeField] private Button _settingsButton;
protected override void OnInitialized()
{
_playButton.onClick.AddListener(OnPlayClicked);
_settingsButton.onClick.AddListener(OnSettingsClicked);
}
protected override void OnOpened()
{
Debug.Log("Main menu opened!");
// Perform opening animations or setup
}
protected override void OnClosed()
{
Debug.Log("Main menu closed!");
// Cleanup or save state
}
private void OnPlayClicked()
{
Close(destroy: false);
_uiService.OpenUiAsync<GameplayHudPresenter>();
}
private async void OnSettingsClicked()
{
var settings = await _uiService.OpenUiAsync<SettingsPresenter>();
// Settings is now open and ready
}
}
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
_uiService.CloseUi<MainMenuPresenter>();
// Close all UI
_uiService.CloseAllUi();
}
}
The UiPresenter
is the base class for all UI elements in the system. It provides:
- Lifecycle callbacks -
OnInitialized()
,OnOpened()
,OnClosed()
- State management - Track open/closed state
- Service integration - Direct access to UI service
public class BasicPopup : UiPresenter
{
protected override void OnInitialized()
{
// Called once when the presenter is first loaded
// Set up UI elements, subscribe to events
}
protected override void OnOpened()
{
// Called every time the UI is shown
// Start animations, refresh data
}
protected override void OnClosed()
{
// Called when the UI is hidden
// Stop animations, save state
}
}
For UI that needs initialization data:
public struct PlayerProfileData
{
public string PlayerName;
public int Level;
public Sprite Avatar;
}
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
_nameText.text = Data.PlayerName;
_levelText.text = $"Level {Data.Level}";
_avatarImage.sprite = Data.Avatar;
}
}
// Usage
var profileData = new PlayerProfileData
{
PlayerName = "Hero",
Level = 42,
Avatar = avatarSprite
};
await _uiService.OpenUiAsync<PlayerProfilePresenter, PlayerProfileData>(profileData);
UI elements are organized into layers, where higher layer numbers appear on top:
// Configure layers in your UiConfigs asset
// Layer 0: Background UI
// Layer 1: Game HUD
// Layer 2: Menus
// Layer 3: Popups
// Layer 4: System messages
// Close all UI in a specific layer
_uiService.CloseAllUi(layer: 2);
Group related UI elements for batch operations:
// Define UI sets in your UiConfigs
// Set 1: Main Menu Set (logo, menu, background)
// Set 2: Gameplay Set (HUD, minimap, controls)
// Set 3: Shop Set (shop window, inventory, currency display)
// Load entire UI set
var loadTasks = _uiService.LoadUiSetAsync(setId: 2);
await UniTask.WhenAll(loadTasks);
// Close entire UI set
_uiService.CloseAllUiSet(setId: 2);
// Unload UI set from memory
_uiService.UnloadUiSet(setId: 2);
Configure your UI in the UiConfigs
ScriptableObject:
- Type - The presenter class type
- Prefab Reference - Addressable reference to the UI prefab
- Layer - Which layer the UI belongs to
- Load Synchronously - Whether to load synchronously (use sparingly)
- UI Set ID - Optional grouping ID
public class NotificationPresenter : UiPresenter
{
[SerializeField] private TextMeshProUGUI _messageText;
[SerializeField] private float _displayDuration = 3f;
public void SetMessage(string message)
{
_messageText.text = message;
}
protected override void OnOpened()
{
// Auto-close after duration
StartCoroutine(AutoClose());
}
private IEnumerator AutoClose()
{
yield return new WaitForSeconds(_displayDuration);
Close(destroy: false);
}
}
For UI with opening/closing animations:
public class AnimatedPopup : DelayUiPresenter
{
[SerializeField] private Animator _animator;
[SerializeField] private float _openDuration = 0.5f;
[SerializeField] private float _closeDuration = 0.3f;
protected override void ConfigureDelayers()
{
// Use animation-based delays
OpeningDelayer = new AnimationDelayer(_animator, "Open");
ClosingDelayer = new AnimationDelayer(_animator, "Close");
// Or use time-based delays
// OpeningDelayer = new TimeDelayer(_openDuration);
// ClosingDelayer = new TimeDelayer(_closeDuration);
}
protected override void OnOpened()
{
base.OnOpened();
// UI is fully open and animation complete
}
}
// Load UI into memory without opening
var ui = await _uiService.LoadUiAsync<InventoryPresenter>();
// Load and immediately open
var ui = await _uiService.LoadUiAsync<InventoryPresenter>(openAfter: true);
// Check if loaded
if (_uiService.LoadedPresenters.ContainsKey(typeof(InventoryPresenter)))
{
// UI is loaded in memory
}
// Unload from memory
_uiService.UnloadUi<InventoryPresenter>();
// Open UI (loads if necessary)
var shop = await _uiService.OpenUiAsync<ShopPresenter>();
// Open with data
var questData = new QuestData { QuestId = 101, Title = "Dragon Slayer" };
await _uiService.OpenUiAsync<QuestPresenter, QuestData>(questData);
// Close UI (keeps in memory)
_uiService.CloseUi<ShopPresenter>();
// Close and destroy
_uiService.CloseUi<ShopPresenter>(destroy: true);
// Get UI if loaded
var hud = _uiService.GetUi<GameHudPresenter>();
// Load all UI in a set
var loadTasks = _uiService.LoadUiSetAsync(setId: 1);
var presenters = await UniTask.WhenAll(loadTasks);
// Add runtime UI to service
var dynamicUi = Instantiate(uiPrefab);
_uiService.AddUi(dynamicUi, layer: 3, openAfter: true);
// Remove UI set and get removed presenters
var removedPresenters = _uiService.RemoveUiSet(setId: 2);
foreach (var presenter in removedPresenters)
{
Destroy(presenter.gameObject);
}
All async operations use UniTask for better performance and WebGL support:
// Sequential loading
var menu = await _uiService.OpenUiAsync<MainMenuPresenter>();
var settings = await _uiService.OpenUiAsync<SettingsPresenter>();
// Parallel loading
var menuTask = _uiService.OpenUiAsync<MainMenuPresenter>();
var hudTask = _uiService.OpenUiAsync<GameHudPresenter>();
await UniTask.WhenAll(menuTask, hudTask);
// With cancellation
var cts = new CancellationTokenSource();
try
{
await _uiService.OpenUiAsync<LoadingPresenter>()
.AttachExternalCancellation(cts.Token);
}
catch (OperationCanceledException)
{
Debug.Log("UI loading was cancelled");
}
The DelayUiPresenter
class provides built-in support for opening/closing animations:
public class SlideInPanel : DelayUiPresenter
{
[SerializeField] private RectTransform _panel;
[SerializeField] private float _slideDuration = 0.5f;
protected override void ConfigureDelayers()
{
OpeningDelayer = new TimeDelayer(_slideDuration);
ClosingDelayer = new TimeDelayer(_slideDuration);
}
protected override void OnPreOpen()
{
// Position panel off-screen
_panel.anchoredPosition = new Vector2(-1000, 0);
}
protected override void OnOpening()
{
// Animate panel sliding in
_panel.DOAnchorPosX(0, _slideDuration);
}
protected override void OnClosing()
{
// Animate panel sliding out
_panel.DOAnchorPosX(-1000, _slideDuration);
}
}
For UI Toolkit (UI Elements) support:
public class UIToolkitMenu : UiToolkitPresenter
{
[SerializeField] private UIDocument _document;
private Button _playButton;
private Label _titleLabel;
protected override void OnInitialized()
{
var root = _document.rootVisualElement;
_playButton = root.Q<Button>("play-button");
_titleLabel = root.Q<Label>("title-label");
_playButton.clicked += OnPlayClicked;
}
private void OnPlayClicked()
{
Close(destroy: false);
}
}
The package includes several helper components:
Automatically adjusts UI for device safe areas (notches, rounded corners):
// Add SafeAreaHelperView component to UI panels that should respect safe areas
[RequireComponent(typeof(RectTransform))]
public class SafeAreaPanel : MonoBehaviour
{
void Awake()
{
gameObject.AddComponent<SafeAreaHelperView>();
}
}
Optimize UI performance by making non-visible graphics non-rendering:
// Add to invisible UI elements that need to block raycasts
gameObject.AddComponent<NonDrawingView>();
Responsive UI that adjusts to screen size:
// Add AdjustScreenSizeFitterView for responsive layouts
var fitter = gameObject.AddComponent<AdjustScreenSizeFitterView>();
// Configure min/max sizes in the inspector
Make text elements interactive with clickable links:
// Add InteractableTextView to TextMeshPro components
var interactableText = gameObject.AddComponent<InteractableTextView>();
// Supports <link> tags in TextMeshPro for clickable URLs
<root>
├── package.json # Package manifest with dependencies and metadata
├── README.md # This documentation file
├── CHANGELOG.md # Version history and release notes
├── LICENSE.md # MIT license terms
├── Runtime/ # Core runtime scripts for the UI service
│ ├── *.asmdef # Assembly definition for runtime code
│ ├── Core Services # Main UI service implementation and interfaces
│ ├── Presenters # Base classes for UI presenters and UI Toolkit support
│ ├── Configuration # ScriptableObject configs for UI setup
│ ├── Asset Loading # Addressables integration and loading utilities
│ ├── Delayers/ # Animation and time-based delay implementations
│ └── Views/ # Helper components for responsive and optimized UI
└── Editor/ # Unity Editor extensions and custom inspectors
├── *.asmdef # Assembly definition for editor code
├── Config Editors # Custom inspectors for UI configuration assets
└── View Editors # Custom editors for specialized UI components
This package requires:
- Unity Addressables (v2.6.0+) - For async asset loading
- UniTask (v2.5.10+) - For efficient async operations
Dependencies are automatically resolved when installing via Unity Package Manager.
We welcome contributions from the community! Here's how you can help:
- Use the GitHub Issues page
- Include your Unity version, package version, and reproduction steps
- Attach relevant code samples, error logs, or screenshots
- Fork the repository on GitHub
- Clone your fork:
git clone https://github.com/yourusername/com.gamelovers.uiservice.git
- Create a feature branch:
git checkout -b feature/amazing-feature
- Make your changes with tests
- Commit:
git commit -m 'Add amazing feature'
- Push:
git push origin feature/amazing-feature
- Create a Pull Request
- Follow C# Coding Conventions
- Add XML documentation to all public APIs
- Include unit tests for new features
- Maintain backward compatibility when possible
- Update CHANGELOG.md for notable changes
- Ensure all tests pass
- Update documentation if needed
- Add changelog entry if applicable
- Request review from maintainers
- API Reference: See inline XML documentation in code
- Examples: Check sample implementations in this README
- Changelog: See CHANGELOG.md for version history
- Issues: Report bugs or request features
- Discussions: Ask questions and share ideas
- Follow @CoderGamester for updates
- Star the repository if you find it useful
- Share your projects using this package
This project is licensed under the MIT License - see the LICENSE.md file for details.
Made with ❤️ for the Unity community
If this package helps your project, please consider giving it a ⭐ on GitHub!