Skip to content

Commit 61850d8

Browse files
committed
Add UiInputBox
1 parent d081a13 commit 61850d8

File tree

2 files changed

+90
-0
lines changed

2 files changed

+90
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using UnityEngine;
3+
using UnityEngine.UI;
4+
5+
public class UiInputBox : UiDialog
6+
{
7+
public delegate void InputResultDelegate(string input);
8+
9+
public enum ButtonType
10+
{
11+
Ok,
12+
OkCancel,
13+
}
14+
15+
public Text MessageText;
16+
public InputField ValueInput;
17+
public Button[] Buttons;
18+
19+
public static UiDialogHandle Show(
20+
string msg, string value = null, ButtonType buttonType = ButtonType.Ok,
21+
InputResultDelegate callback = null, string customOkName = null)
22+
{
23+
UiDialogHandle handle;
24+
{
25+
var builtin = UiManager.Instance.FindFromDialogRoot("InputBox");
26+
if (builtin != null)
27+
{
28+
handle = UiManager.Instance.ShowModalTemplate(builtin.gameObject);
29+
}
30+
else
31+
{
32+
var msgBoxPrefab = Resources.Load("InputBox") as GameObject;
33+
handle = UiManager.Instance.ShowModalPrefab(msgBoxPrefab);
34+
}
35+
}
36+
37+
if (callback != null)
38+
handle.Hidden += (dlg, returnValue) => callback((string)returnValue);
39+
40+
var msgBox = (UiInputBox)handle.Dialog;
41+
msgBox.MessageText.text = msg;
42+
msgBox.ValueInput.text = value ?? "";
43+
44+
var b0 = msgBox.Buttons[0];
45+
var b0Text = b0.transform.Find("Text").GetComponent<Text>();
46+
var b1 = msgBox.Buttons[1];
47+
var b1Text = b1.transform.Find("Text").GetComponent<Text>();
48+
49+
b1.gameObject.SetActive(buttonType != ButtonType.Ok);
50+
51+
switch (buttonType)
52+
{
53+
case ButtonType.Ok:
54+
b0Text.text = customOkName ?? "Ok";
55+
b0.onClick.AddListener(() => msgBox.OnButtonClick(true));
56+
break;
57+
58+
case ButtonType.OkCancel:
59+
b0Text.text = customOkName ?? "Ok";
60+
b0.onClick.AddListener(() => msgBox.OnButtonClick(true));
61+
b1Text.text = "Cancel";
62+
b1.onClick.AddListener(() => msgBox.OnButtonClick(false));
63+
break;
64+
}
65+
66+
return handle;
67+
}
68+
69+
private void OnButtonClick(bool ok)
70+
{
71+
Hide(ok ? ValueInput.text : null);
72+
}
73+
74+
public void OnInputSubmit()
75+
{
76+
OnButtonClick(true);
77+
}
78+
}

src/UnityPackage/Assets/UiManager/UiInputBox.cs.meta

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)