Class Toast
A temporary notification that appears at the edge of the screen to display brief messages.
Inherited Members
Namespace: Unity.AppUI.UI
Assembly: Unity.AppUI.dll
Syntax
public sealed class Toast : PopupNotification<Toast>
Remarks
Toast notifications are brief messages that appear temporarily at the edge of the screen to inform users about the status of operations or provide feedback. They automatically disappear after a set duration or can include action buttons for user interaction.
Toasts are ideal for displaying confirmations, status updates, error messages, or other non-critical information that doesn't require immediate user attention. They don't interrupt the user's workflow and appear unobtrusively.
The toast component supports different visual styles (informative, positive, negative, warning) and can include icons, text, and optional action buttons. Multiple toasts can be stacked when several notifications need to be displayed.
Use toasts for brief feedback messages. For critical information that requires user action, consider using modals or alert dialogs instead.
Anatomy
Toast Variants:
<appui:Toast message="This is an informative message" variant="Info" size="M" />
<appui:Toast message="Operation completed successfully" variant="Positive" size="M" />
<appui:Toast message="Warning: Please check your input" variant="Warning" size="M" />
<appui:Toast message="Error: Something went wrong" variant="Negative" size="M" />
With Actions:
<appui:Toast message="File saved successfully" variant="Positive" action-text="Undo" size="M" />
<appui:Toast message="Connection lost" variant="Warning" action-text="Retry" size="M" />
With Icons:
<appui:Toast message="Download complete" icon="download" variant="Positive" size="M" />
<appui:Toast message="Update available" icon="update" variant="Info" size="M" />
Examples
Basic toast notifications: Creating simple toast messages with different styles
// Simple success message
var successToast = Toast.Build(rootElement, "File saved successfully!", NotificationDuration.Medium)
.SetStyle(NotificationStyle.Positive)
.SetIcon("check");
successToast.Show();
// Error message
var errorToast = Toast.Build(rootElement, "Failed to connect to server", NotificationDuration.Long)
.SetStyle(NotificationStyle.Negative)
.SetIcon("warning");
errorToast.Show();
// Information message
var infoToast = Toast.Build(rootElement, "New update available", NotificationDuration.Short)
.SetStyle(NotificationStyle.Informative)
.SetIcon("info");
infoToast.Show();
Toast with action buttons: Adding interactive buttons to toast notifications
var undoToast = Toast.Build(rootElement, "Item deleted", NotificationDuration.Long)
.SetStyle(NotificationStyle.Default)
.SetIcon("trash")
.AddAction(1, "Undo", (toast) => {
RestoreDeletedItem();
toast.Dismiss();
}, true)
.AddAction(2, "View All", (toast) => {
OpenTrashView();
toast.Dismiss();
}, true);
undoToast.Show();
// Toast with multiple actions
var downloadToast = Toast.Build(rootElement, "Download complete", NotificationDuration.Indefinite)
.SetStyle(NotificationStyle.Positive)
.SetIcon("download")
.AddAction(1, "Open", (toast) => OpenFile())
.AddAction(2, "Show in Folder", (toast) => ShowInExplorer());
downloadToast.Show();
Managing toast lifecycle: Controlling toast display duration and dismissal
// Create toast with indefinite duration
var progressToast = Toast.Build(rootElement, "Processing...", NotificationDuration.Indefinite)
.SetStyle(NotificationStyle.Informative)
.SetIcon("loading");
// Show the toast
progressToast.Show();
// Update the toast content during operation
ProcessFileAsync().ContinueWith(task => {
if (task.IsCompletedSuccessfully)
{
progressToast
.SetText("Processing complete!")
.SetStyle(NotificationStyle.Positive)
.SetIcon("check");
// Auto-dismiss after 3 seconds
ScheduleDismissal(progressToast, 3000);
}
else
{
progressToast
.SetText("Processing failed")
.SetStyle(NotificationStyle.Negative)
.SetIcon("error");
}
});
Properties
icon
The icon used inside the Toast as leading UI element.
Declaration
public string icon { get; }
Property Value
| Type | Description |
|---|---|
| string |
style
Returns the styling used by the bar. See NotificationStyle for more information.
Declaration
public NotificationStyle style { get; }
Property Value
| Type | Description |
|---|---|
| NotificationStyle |
text
Returns the raw message or Localization dictionary key used by the bar.
Declaration
public string text { get; }
Property Value
| Type | Description |
|---|---|
| string |
Methods
AddAction(int, string, Action<Toast>, bool)
Add an Action to display in the Toast bar.
Declaration
public Toast AddAction(int actionId, string message, Action<Toast> callback, bool autoDismiss = true)
Parameters
| Type | Name | Description |
|---|---|---|
| int | actionId | The Action ID, which is a unique identifier for your action. |
| string | message | The raw message or Localization dictionary key for the action to be displayed. |
| Action<Toast> | callback | The callback which will be called when the action is triggered. |
| bool | autoDismiss | Whether the toast should be dismissed automatically after the action is triggered. |
Returns
| Type | Description |
|---|---|
| Toast | The Toast instance, if no exception has occured. |
Build(VisualElement, string, NotificationDuration)
Build and return a Toast UI element.
The method will find the best suitable parent view which will contain the Toast element.
Declaration
public static Toast Build(VisualElement referenceView, string text, NotificationDuration duration)
Parameters
| Type | Name | Description |
|---|---|---|
| VisualElement | referenceView | An arbitrary VisualElement which is currently present in the UI panel. |
| string | text | The raw message or Localization dictionary key for the message to be displayed inside the Toast. |
| NotificationDuration | duration | A duration enum value. |
Returns
| Type | Description |
|---|---|
| Toast | The Toast instance, if no exception has occured. |
Remarks
The snackbar is not displayed directly, you have to call Show().
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | If |
HideView(DismissType)
Called when it is time to hide the popup.
Declaration
protected override void HideView(DismissType reason)
Parameters
| Type | Name | Description |
|---|---|---|
| DismissType | reason | The reason why the popup should be dismissed. |
Overrides
InvokeShownEventHandlers()
Called when the popup has become visible. This method will invoke any handlers attached to the shown event.
Declaration
protected override void InvokeShownEventHandlers()
Overrides
RemoveAction(int)
Remove an already existing action.
Declaration
public Toast RemoveAction(int actionId)
Parameters
| Type | Name | Description |
|---|---|---|
| int | actionId | The Action ID. |
Returns
| Type | Description |
|---|---|
| Toast | The Toast instance, if no exception has occured. |
SetIcon(string)
Set a new value for the icon property.
Declaration
public Toast SetIcon(string iconName)
Parameters
| Type | Name | Description |
|---|---|---|
| string | iconName | The name of the icon. |
Returns
| Type | Description |
|---|---|
| Toast | The Toast to continuously build the element. |
SetStyle(NotificationStyle)
Set the styling used by the bar.
Declaration
public Toast SetStyle(NotificationStyle notificationStyle)
Parameters
| Type | Name | Description |
|---|---|---|
| NotificationStyle | notificationStyle | A notification style enum value. See NotificationStyle for more information. |
Returns
| Type | Description |
|---|---|
| Toast | The Toast to continuously build the element. |
SetText(string)
Update the text in the Toast.
Declaration
public Toast SetText(string txt)
Parameters
| Type | Name | Description |
|---|---|---|
| string | txt | The raw message or Localization dictionary key for the message to be displayed. |
Returns
| Type | Description |
|---|---|
| Toast | The Toast to continuously build the element. |