Dialog
Options
Buttons Templates
ReadOnlyCollection<Button>
Templates for the buttons.
Content Root
RectTransform
Root 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
DialogInfoBase
Component to display the dialog info.
AutoFocus
bool
Set focus to the last Selectable object in the Dialog.
Close Button
Button
Button to close dialog.
Buttons Container
RectTransform
Buttons container. If container not specified will be used parent of the button template.
Hide on Modal Click
bool
Close dialog on click on the background if the
modal
option enabled.
Show() Method Parameters
title
and message
also can be specified with SetInfo()
title
string
Dialog title.Can be changed withSetInfo()
method.message
string
Dialog message.Can be changed withSetInfo()
method.buttons
ButtonsPool
Dialog buttons.Can be changed withSetButtons()
method.DialogButton
fields:Label
string
Button label.
Action
Func<DialogBase, int, bool>
Function to run on button click. Receive dialog instance and button index, return
true
to close dialog; otherwisefalse
.Template Index
int
Index of the button template.
focusButton
string
Button with focus by default.Can be changed withSetButtons()
orFocusButton()
.position
Vector3?
Dialog position.Can be changed withSetPosition()
.icon
Sprite
Dialog icon.Can be changed withSetInfo()
method.modal
bool
Modal dialog.Can be changed withSetModal()
.modalSprite
Sprite
Background image for the modal dialog.Can be changed withSetModal()
.modalColor
Color?
Background color for the modal dialog.Can be changed withSetModal()
.canvas
Canvas
Canvas to display dialog. Required if dialog template is prefab.Can be changed withSetCanvas()
.content
RectTransform
Dialog content. Can be used instead of the message and icon.Can be changed withSetContent()
.onClose
Action
Action to run when dialog closed.Can be changed withOnClose
field.onCancel
Func<int, bool>
Function to run when dialog canceled. Receive dialog instance and -1as button index, returntrue
if dialog should be closed.Obsolete, useFunc<DialogBase, int, bool> OnDialogCancel
field instead.
ShowAsync() Method Parameters
title
and message
also can be specified with SetInfo()
-1
in case of Cancel()
method.title
string
Dialog title.Can be changed withSetInfo()
method.message
string
Dialog message.Can be changed withSetInfo()
method.buttons
ButtonsPool
Dialog buttons.Can be changed withSetButtons()
method.DialogButton
fields:Label
string
Button label.
Action
Func<DialogBase, int, bool>
Function to run on button click. Receive dialog instance and button index, return
true
to close dialog; otherwisefalse
.Template Index
int
Index of the button template.
focusButton
string
Button with focus by default.Can be changed withSetButtons()
orFocusButton()
.position
Vector3?
Dialog position.Can be changed withSetPosition()
.icon
Sprite
Dialog icon.Can be changed withSetInfo()
method.modal
bool
Modal dialog.Can be changed withSetModal()
.modalSprite
Sprite
Background image for the modal dialog.Can be changed withSetModal()
.modalColor
Color?
Background color for the modal dialog.Can be changed withSetModal()
.canvas
Canvas
Canvas to display dialog. Required if dialog template is prefab.Can be changed withSetCanvas()
.content
RectTransform
Dialog content. Can be used instead of the message and icon.Can be changed withSetContent()
.closeOnButtonClick
bool
Close 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>
{
// ...
}