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

All parameters are optional.
title and message also can be specified with SetInfo()
to use formatted strings.
  • title string

    Dialog title.
    Can be changed with SetInfo() method.
  • message string

    Dialog message.
    Can be changed with SetInfo() method.
  • buttons ButtonsPool

    Dialog buttons.
    Can be changed with SetButtons() 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; otherwise false.

    • Template Index int

      Index of the button template.

  • focusButton string

    Button with focus by default.
    Can be changed with SetButtons() or FocusButton().
  • position Vector3?

    Dialog position.
    Can be changed with SetPosition().
  • icon Sprite

    Dialog icon.
    Can be changed with SetInfo() method.
  • modal bool

    Modal dialog.
    Can be changed with SetModal().
  • modalSprite Sprite

    Background image for the modal dialog.
    Can be changed with SetModal().
  • modalColor Color?

    Background color for the modal dialog.
    Can be changed with SetModal().
  • canvas Canvas

    Canvas to display dialog. Required if dialog template is prefab.
    Can be changed with SetCanvas().
  • content RectTransform

    Dialog content. Can be used instead of the message and icon.
    Can be changed with SetContent().
  • onClose Action

    Action to run when dialog closed.
    Can be changed with OnClose field.
  • onCancel Func<int, bool>

    Function to run when dialog canceled. Receive dialog instance and -1
    as button index, return true if dialog should be closed.
    Obsolete, use Func<DialogBase, int, bool> OnDialogCancel
    field instead.

ShowAsync() Method Parameters

All parameters are optional.
title and message also can be specified with SetInfo()
to use formatted strings.
Returns index of the clicked button or -1 in case of Cancel() method.
  • title string

    Dialog title.
    Can be changed with SetInfo() method.
  • message string

    Dialog message.
    Can be changed with SetInfo() method.
  • buttons ButtonsPool

    Dialog buttons.
    Can be changed with SetButtons() 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; otherwise false.

    • Template Index int

      Index of the button template.

  • focusButton string

    Button with focus by default.
    Can be changed with SetButtons() or FocusButton().
  • position Vector3?

    Dialog position.
    Can be changed with SetPosition().
  • icon Sprite

    Dialog icon.
    Can be changed with SetInfo() method.
  • modal bool

    Modal dialog.
    Can be changed with SetModal().
  • modalSprite Sprite

    Background image for the modal dialog.
    Can be changed with SetModal().
  • modalColor Color?

    Background color for the modal dialog.
    Can be changed with SetModal().
  • canvas Canvas

    Canvas to display dialog. Required if dialog template is prefab.
    Can be changed with SetCanvas().
  • content RectTransform

    Dialog content. Can be used instead of the message and icon.
    Can be changed with SetContent().
  • 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

  1. 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;
       }
    }
    
  2. 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>
{
   // ...
}