Luna UI provides a complete notification system with support for toast notifications, notification history, and custom actions.
The notification system consists of several interconnected classes:
The data model for notifications.
public class NotificationData
{
public Guid Id;
public string Title;
public string Body;
public INotificationImageSource ImageSource;
public bool IsDismissed;
public NotificationSize Size;
public NotificationAction OnClickAction;
}| Property | Type | Description |
|---|---|---|
| Id | Guid | Unique identifier (auto-generated) |
| Title | string | Notification title (optional) |
| Body | string | Notification body text (optional) |
| ImageSource | INotificationImageSource | Image source interface |
| IsDismissed | bool | Whether the notification has been dismissed |
| Size | NotificationSize | Display size (Small, Default, Large) |
| OnClickAction | NotificationAction | Custom action to execute on click |
public NotificationData(
string title,
string body,
INotificationImageSource imageSource,
NotificationSize size
)public enum NotificationSize
{
Small,
Default,
Large
}Manages the notification history with a configurable maximum size.
| Property | Type | Description |
|---|---|---|
| History | ReadOnlyCollection<NotificationData> | Read-only access to history |
| MaxNotifications | int | Maximum notifications to store (default: 20) |
Adds a new notification to history.
public void PushNotification(NotificationData notificationData)Gets the most recent non-dismissed notifications.
public List<NotificationData> GetLastNonDismissed(int amount)Retrieves a notification by ID.
public NotificationData GetNotification(Guid id)Clears all notifications from history.
public void ClearHistory()Fired when a new notification is pushed.
public event Action<Guid> OnPushNotification;Creates and manages a single toast notification UI element.
public SingleNotification(
VisualElement parent,
NotificationData notificationData,
UIColorName color
)| Parameter | Type | Description |
|---|---|---|
| parent | VisualElement | The notification container element |
| notificationData | NotificationData | The notification data |
| color | UIColorName | Color theme for the dismiss button |
The notification template must contain:
"Title""Body""Image""DismissButton"Fired when the notification is dismissed.
public event Action OnDismiss;The following classes are automatically applied:
| Class | Condition |
|---|---|
notification | Always applied |
notification-sm | When Size is Small |
notification-lg | When Size is Large |
Interface for providing notification images.
public interface INotificationImageSource
{
Sprite GetImage();
}[Serializable]
public class SpriteImageSource : INotificationImageSource
{
[SerializeField] private Sprite _sprite;
public Sprite GetImage() => _sprite;
}
[Serializable]
public class AddressableImageSource : INotificationImageSource
{
[SerializeField] private AssetReferenceSprite _spriteRef;
private Sprite _loadedSprite;
public async Task LoadAsync()
{
_loadedSprite = await _spriteRef.LoadAssetAsync<Sprite>().Task;
}
public Sprite GetImage() => _loadedSprite;
}Abstract base class for notification click actions.
public abstract class NotificationAction
{
public virtual void Execute(string title, string body);
}[Serializable]
public class OpenURLAction : NotificationAction
{
[SerializeField] private string _url;
public override void Execute(string title, string body)
{
base.Execute(title, body);
Application.OpenURL(_url);
}
}
[Serializable]
public class ShowDetailAction : NotificationAction
{
public override void Execute(string title, string body)
{
base.Execute(title, body);
// Show detail panel with title and body
NotificationDetailPanel.Show(title, body);
}
}A UIViewComponent that displays the notification history in a ListView.
Create a UXML with:
"NotificationList""ReturnButton"Create a notification static template UXML with Title, Body, and Image elements
Assign the template in the inspector
public void Initialize(NotificationHistory notificationHistory)The view automatically:
using System;
using UnityEngine;
using UnityEngine.UIElements;
using CupkekGames.Luna;
public class NotificationManager : MonoBehaviour
{
[SerializeField] private UIDocument _uiDocument;
[SerializeField] private VisualTreeAsset _notificationTemplate;
[SerializeField] private UIColorName _notificationColor = UIColorName.PRIMARY;
private NotificationHistory _history = new NotificationHistory();
private VisualElement _notificationContainer;
private List<SingleNotification> _activeNotifications = new();
private void Start()
{
_notificationContainer = _uiDocument.rootVisualElement.Q<VisualElement>("NotificationContainer");
_history.OnPushNotification += OnNotificationPushed;
}
public void ShowNotification(string title, string body, Sprite image = null)
{
var imageSource = image != null ? new SpriteImageSource(image) : null;
var data = new NotificationData(title, body, imageSource, NotificationSize.Default);
_history.PushNotification(data);
}
private void OnNotificationPushed(Guid id)
{
var data = _history.GetNotification(id);
// Create notification UI
var container = _notificationTemplate.Instantiate();
_notificationContainer.Add(container);
var notification = new SingleNotification(container, data, _notificationColor);
notification.OnDissmiss += () => OnNotificationDismissed(notification, container, data);
_activeNotifications.Add(notification);
// Auto-dismiss after 5 seconds
_uiDocument.rootVisualElement.schedule.Execute(() =>
{
if (!data.IsDismissed)
{
notification.Dismiss();
}
}).ExecuteLater(5000);
}
private void OnNotificationDismissed(
SingleNotification notification,
VisualElement container,
NotificationData data)
{
data.IsDismissed = true;
_activeNotifications.Remove(notification);
_notificationContainer.Remove(container);
}
private void OnDestroy()
{
_history.OnPushNotification -= OnNotificationPushed;
}
}NotificationSize.Small for quick updates, Large for important messagesINotificationImageSource for different asset loading strategiesOnPushNotification to show toast notificationsGetLastNonDismissed() to show a badge count of unread notificationsMaxNotificationsSettings
Theme
Light
Contrast
Material
Dark
Dim
Material Dark
System
Sidebar(Light & Contrast only)
Font Family
DM Sans
Wix
Inclusive Sans
AR One Sans
Direction