Dialog
Note
See Windows Animations for open and closed animations.
Options
Buttons Templates
ReadOnlyCollection<Button>Templates for the buttons.
Content Root
RectTransformRoot gameobject for the content.
Title Text
Text(obsolete)GameObject to display title. Replaced with the DialogInfo.
Content Text
Text(obsolete)GameObject to display text. Replaced with the DialogInfo.
Icon
Image(obsolete)GameObject to display icon. Replaced with the DialogInfo.
Dialog Info
DialogInfoBaseComponent to display the dialog info.
AutoFocus
boolSet focus to the last Selectable object in the Dialog.
Close Button
ButtonButton to close dialog.
Buttons Container
RectTransformButtons container. If container not specified will be used parent of the button template.
Hide on Modal Click
boolClose dialog on click on the background if the
modaloption enabled.
Events
OnAnimationStart
UnityEvent<bool>The event is raised before the animation starts.
Arguments: true if opening animation; false if closing animation.
Show() Method Parameters
title and message also can be specified with SetInfo()title
stringDialog title.Can be changed withSetInfo()method.message
stringDialog message.Can be changed withSetInfo()method.buttons
ButtonsPoolDialog buttons.Can be changed withSetButtons()method.DialogButtonfields:Label
stringButton label.
Action
Func<DialogBase, int, bool>Function to run on button click. Receive dialog instance and button index, return
trueto close dialog; otherwisefalse.Template Index
intIndex of the button template.
focusButton
stringButton with focus by default.Can be changed withSetButtons()orFocusButton().position
Vector3?Dialog position.Can be changed withSetPosition().icon
SpriteDialog icon.Can be changed withSetInfo()method.modal
boolModal dialog.Can be changed withSetModal().modalSprite
SpriteBackground image for the modal dialog.Can be changed withSetModal().modalColor
Color?Background color for the modal dialog.Can be changed withSetModal().canvas
CanvasCanvas to display dialog. Required if dialog template is prefab.Can be changed withSetCanvas().content
RectTransformDialog content. Can be used instead of the message and icon.Can be changed withSetContent().onClose
ActionAction to run when dialog closed.Can be changed withOnClosefield.onCancel
Func<int, bool>Function to run when dialog canceled. Receive dialog instance and -1as button index, returntrueif dialog should be closed.Obsolete, useFunc<DialogBase, int, bool> OnDialogCancelfield instead.
ShowAsync() Method Parameters
title and message also can be specified with SetInfo()-1 in case of Cancel() method.title
stringDialog title.Can be changed withSetInfo()method.message
stringDialog message.Can be changed withSetInfo()method.buttons
ButtonsPoolDialog buttons.Can be changed withSetButtons()method.DialogButtonfields:Label
stringButton label.
Action
Func<DialogBase, int, bool>Function to run on button click. Receive dialog instance and button index, return
trueto close dialog; otherwisefalse.Template Index
intIndex of the button template.
focusButton
stringButton with focus by default.Can be changed withSetButtons()orFocusButton().position
Vector3?Dialog position.Can be changed withSetPosition().icon
SpriteDialog icon.Can be changed withSetInfo()method.modal
boolModal dialog.Can be changed withSetModal().modalSprite
SpriteBackground image for the modal dialog.Can be changed withSetModal().modalColor
Color?Background color for the modal dialog.Can be changed withSetModal().canvas
CanvasCanvas to display dialog. Required if dialog template is prefab.Can be changed withSetCanvas().content
RectTransformDialog content. Can be used instead of the message and icon.Can be changed withSetContent().closeOnButtonClick
boolClose dialog on button click.
Minimal code
// create dialog instance
var dialog = dialogTemplate.Clone();
// show dialog
dialog.Show();
// specify root canvas if dialog cloned from prefab
dialog.Show(canvas: canvas);
Advanced
// create dialog instance
var dialog = dialogPrefab.Clone();
// show dialog with following parameters
dialog.Show(
title: "Modal Dialog",
message: "Simple Modal Dialog.",
buttons: new DialogButton[]
{
new DialogButton(
"Close", // label
DialogBase.DefaultClose, // Func<DialogBase, int, bool>, receive dialog instance and button index, return true to close dialog, otherwise false
0 // button index in ButtonsTemplates
),
},
focusButton: "Close",
modal: true,
modalColor: new Color(0, 0, 0, 0.8f)
);
Async
// create dialog instance
var dialog = dialogPrefab.Clone();
// show dialog with following parameters
var button_index = await dialog.ShowAsync(
title: "Modal Dialog",
message: "Simple Modal Dialog.",
buttons: new DialogButton[]
{
"Do Some Action",
"Do Other Action",
"Close",
},
focusButton: "Close",
modal: true,
modalColor: new Color(0, 0, 0, 0.8f)
);
if (button_index == 0)
{
Debug.Log("Do Some Action");
}
else if (button_index == 1)
{
Debug.Log("Do Other Action");
}
Adding new behaviour
Create helper component
using UnityEngine; using UnityEngine.UI; public class DialogInputHelper : MonoBehaviour { [SerializeField] public InputField Username; [SerializeField] public InputField Password; // Reset values public void Refresh() { Username.text = ""; Password.text = ""; } public bool Validate() { var valid_username = Username.text.Trim().Length > 0; var valid_password = Password.text.Length > 0; if (!valid_username) { Username.Select(); } else if (!valid_password) { Password.Select(); } return valid_username && valid_password; } }
Show dialog.
public void ShowDialogSignIn() { var dialog = dialogSignIn.Clone(); var helper = dialog.GetComponent<DialogInputHelper>(); helper.Refresh(); dialog.Show( title: "Sign into your Account", buttons: new DialogButton[] { // on click call SignInNotify new DialogButton("Sign in", SignInNotify), // on click close dialog new DialogButton("Cancel"), }, focusButton: "Sign in", modal: true, modalColor: new Color(0, 0, 0, 0.8f) ); } bool SignInNotify(DialogBase dialog, int index) { var helper = dialog.GetComponent<DialogInputHelper>(); if (!helper.Validate()) { return false; } //show notification var message = "Sign in.\nUsername: " + helper.Username.text + "\nPassword: <hidden>"; notifySample.Clone().Show(message, customHideDelay: 3f); return true; }
Custom Dialogs
You can create derived class with own methods and fields.
public class MyDialog : DialogCustom<MyDialog>
{
// ...
}