Simple system for creating modal windows with callbacks in Unity, with extensible template.
This code
SimpleModalWindow.Create()
.SetHeader("Simple Modal")
.SetBody("Here goes body")
.Show();
Gives you this simple dismissable modal window:
You can also add buttons with callbacks and make modal window not dismissable:
SimpleModalWindow.Create(ignorable: false)
.SetHeader("Modal With Buttons")
.SetBody("You must press one of the buttons")
.AddButton("Yes", () => print("Yes pressed"), ModalButtonType.Success)
.AddButton("No", () => print("No pressed"), ModalButtonType.Danger)
.Show();
Which will lead you to this result:
You can make your own modal windows, for example, modal with input field creation simply as following:
- Create script and inherit it from
ModalWindow<T>
using System;
using UnityEngine;
using UnityEngine.UI;
public class InputModalWindow : ModalWindow<InputModalWindow>
{
[SerializeField] private InputField inputField;
private Action<string> onInputFieldDone;
public InputModalWindow SetInputField(Action<string> onDone)
{
onInputFieldDone = onDone;
return this;
}
void SubmitInput()
{
onInputFieldDone?.Invoke(inputField.text);
onInputFieldDone = null;
Close();
}
public void UI_InputFieldOKButton()
{
SubmitInput();
}
}
- Create prefab for it (use original modal window prefab as template) and place it under
Resources/Modal Windows
- Use it from your code in this way:
ModalWindow<InputModalWindow>.Create()
.SetHeader("Input Field Modal")
.SetBody("Enter something:")
.SetInputField((inputResult) => print("Text: " + inputResult))
.Show();
Result:
Option 1:
Download latest .unitypackage
release from the releases tab and import it in your project.
Option 2:
Download .zip of this repository and extract all files from Assets/ModalWindows
to your project folder.
Thanks for using this asset, i will be pleasured to see any feedback :)