|
| 1 | +# UnityModalWindows |
| 2 | +Simple system for creating modal windows with callbacks in Unity, with extensible template. |
| 3 | + |
| 4 | +# In action |
| 5 | +## Simple modal windows |
| 6 | +This code |
| 7 | + |
| 8 | +```C# |
| 9 | +SimpleModalWindow.Create() |
| 10 | + .SetHeader("Simple Modal") |
| 11 | + .SetBody("Here goes body") |
| 12 | + .Show(); |
| 13 | +``` |
| 14 | + |
| 15 | +Gives you this simple dismissable modal window: |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +You can also add buttons with callbacks and make modal window not dismissable: |
| 20 | + |
| 21 | +```C# |
| 22 | +SimpleModalWindow.Create(ignorable: false) |
| 23 | + .SetHeader("Modal With Buttons") |
| 24 | + .SetBody("You must press one of the buttons") |
| 25 | + .AddButton("Yes", () => print("Yes pressed"), ModalButtonType.Success) |
| 26 | + .AddButton("No", () => print("No pressed"), ModalButtonType.Danger) |
| 27 | + .Show(); |
| 28 | +``` |
| 29 | + |
| 30 | +Which will lead you to this result: |
| 31 | + |
| 32 | + |
| 33 | +## Custom modals |
| 34 | + |
| 35 | +You can make your own modal windows, for example, modal with input field creation simply as following: |
| 36 | + |
| 37 | +1. Create script and inherit it from `ModalWindow<T>` |
| 38 | +```C# |
| 39 | +using System; |
| 40 | +using UnityEngine; |
| 41 | +using UnityEngine.UI; |
| 42 | + |
| 43 | +public class InputModalWindow : ModalWindow<InputModalWindow> |
| 44 | +{ |
| 45 | + [SerializeField] private InputField inputField; |
| 46 | + private Action<string> onInputFieldDone; |
| 47 | + |
| 48 | + public InputModalWindow SetInputField(Action<string> onDone) |
| 49 | + { |
| 50 | + onInputFieldDone = onDone; |
| 51 | + return this; |
| 52 | + } |
| 53 | + |
| 54 | + void SubmitInput() |
| 55 | + { |
| 56 | + onInputFieldDone?.Invoke(inputField.text); |
| 57 | + onInputFieldDone = null; |
| 58 | + Close(); |
| 59 | + } |
| 60 | + |
| 61 | + public void UI_InputFieldOKButton() |
| 62 | + { |
| 63 | + SubmitInput(); |
| 64 | + } |
| 65 | +} |
| 66 | +``` |
| 67 | + |
| 68 | +2. Create prefab for it (use original modal window prefab as template) and place it under `Resources/Modal Windows` |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | +3. Use it from your code in this way: |
| 73 | +```C# |
| 74 | +ModalWindow<InputModalWindow>.Create() |
| 75 | + .SetHeader("Input Field Modal") |
| 76 | + .SetBody("Enter something:") |
| 77 | + .SetInputField((inputResult) => print("Text: " + inputResult)) |
| 78 | + .Show(); |
| 79 | +``` |
| 80 | + |
| 81 | +4. (Optional) Simplify creation by redefining `Create` method inside `InputModalWindow` class: |
| 82 | +```C# |
| 83 | +public static new InputModalWindow Create(bool ignorable = true) |
| 84 | +{ |
| 85 | + return ModalWindow<InputModalWindow>.Create(ignorable); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +So you will be able to call `Create` from it directly: |
| 90 | +```C# |
| 91 | +InputModalWindow.Create() |
| 92 | + .SetHeader("Input Field Modal") |
| 93 | + .SetBody("Enter something:") |
| 94 | + .SetInputField((inputResult) => print("Text: " + inputResult)) |
| 95 | + .Show(); |
| 96 | +``` |
| 97 | + |
| 98 | +# Installation |
| 99 | + |
| 100 | +**Option 1:** |
| 101 | + |
| 102 | +Download latest `.unitypackage` release from the releases tab and import it in your project. |
| 103 | + |
| 104 | +**Option 2:** |
| 105 | + |
| 106 | +Download .zip of this repository and extract all files from `Assets/ModalWindows` to your project folder. |
| 107 | + |
| 108 | +# Conclusion |
| 109 | +Thanks for using this asset, i will be pleasured to see any feedback :) |
0 commit comments