Skip to content

Commit 0562618

Browse files
committed
Add Dialog for Unity Editor
1 parent 90d4b5b commit 0562618

3 files changed

Lines changed: 145 additions & 3 deletions

File tree

Packages/com.github.asus4.nativedialog/Runtime/DialogManager.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ private void Awake()
7272
private IDialog CreateDialog()
7373
{
7474
#if UNITY_EDITOR
75-
var mock = gameObject.AddComponent<DialogMock>();
76-
mock.Initialize(this, true);
77-
return mock;
75+
return new DialogEditor(this);
7876
#elif UNITY_ANDROID
7977
return new DialogAndroid();
8078
#elif UNITY_IOS
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#if UNITY_EDITOR
2+
using System.Collections.Generic;
3+
using UnityEditor;
4+
5+
namespace NativeDialog
6+
{
7+
internal sealed class DialogEditor : IDialog
8+
{
9+
private string decideLabel = "YES";
10+
private string cancelLabel = "NO";
11+
private string closeLabel = "CLOSE";
12+
private int currentId = 0;
13+
private IDialogReceiver receiver;
14+
private readonly Dictionary<int, bool> pendingDialogs = new Dictionary<int, bool>();
15+
16+
public DialogEditor(IDialogReceiver receiver)
17+
{
18+
this.receiver = receiver;
19+
}
20+
21+
public void Dispose()
22+
{
23+
pendingDialogs.Clear();
24+
receiver = null;
25+
}
26+
27+
public void SetLabel(string decide, string cancel, string close)
28+
{
29+
decideLabel = decide;
30+
cancelLabel = cancel;
31+
closeLabel = close;
32+
}
33+
34+
public int ShowSelect(string message)
35+
{
36+
int id = ++currentId;
37+
EditorApplication.delayCall += () => ShowSelectDialog(id, null, message);
38+
return id;
39+
}
40+
41+
public int ShowSelect(string title, string message)
42+
{
43+
int id = ++currentId;
44+
EditorApplication.delayCall += () => ShowSelectDialog(id, title, message);
45+
return id;
46+
}
47+
48+
public int ShowSubmit(string message)
49+
{
50+
int id = ++currentId;
51+
EditorApplication.delayCall += () => ShowSubmitDialog(id, null, message);
52+
return id;
53+
}
54+
55+
public int ShowSubmit(string title, string message)
56+
{
57+
int id = ++currentId;
58+
EditorApplication.delayCall += () => ShowSubmitDialog(id, title, message);
59+
return id;
60+
}
61+
62+
public void Dismiss(int id)
63+
{
64+
if (pendingDialogs.ContainsKey(id))
65+
{
66+
pendingDialogs.Remove(id);
67+
}
68+
}
69+
70+
private void ShowSelectDialog(int id, string title, string message)
71+
{
72+
if (!pendingDialogs.ContainsKey(id))
73+
{
74+
pendingDialogs[id] = true;
75+
}
76+
else if (!pendingDialogs[id])
77+
{
78+
return;
79+
}
80+
81+
bool result;
82+
if (string.IsNullOrEmpty(title))
83+
{
84+
result = EditorUtility.DisplayDialog("", message, decideLabel, cancelLabel);
85+
}
86+
else
87+
{
88+
result = EditorUtility.DisplayDialog(title, message, decideLabel, cancelLabel);
89+
}
90+
91+
if (pendingDialogs.ContainsKey(id))
92+
{
93+
pendingDialogs.Remove(id);
94+
if (result)
95+
{
96+
receiver?.OnSubmit(id.ToString());
97+
}
98+
else
99+
{
100+
receiver?.OnCancel(id.ToString());
101+
}
102+
}
103+
}
104+
105+
private void ShowSubmitDialog(int id, string title, string message)
106+
{
107+
if (!pendingDialogs.ContainsKey(id))
108+
{
109+
pendingDialogs[id] = true;
110+
}
111+
else if (!pendingDialogs[id])
112+
{
113+
return;
114+
}
115+
116+
if (string.IsNullOrEmpty(title))
117+
{
118+
EditorUtility.DisplayDialog("", message, closeLabel);
119+
}
120+
else
121+
{
122+
EditorUtility.DisplayDialog(title, message, closeLabel);
123+
}
124+
125+
if (pendingDialogs.ContainsKey(id))
126+
{
127+
pendingDialogs.Remove(id);
128+
receiver?.OnSubmit(id.ToString());
129+
}
130+
}
131+
}
132+
}
133+
#endif

Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogEditor.cs.meta

Lines changed: 11 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)