Skip to content

Commit 77a80cb

Browse files
authored
Merge pull request #12 from asus4/class-comments
Support Dialog in Editor
2 parents 6ac33f4 + 7b85234 commit 77a80cb

9 files changed

Lines changed: 256 additions & 7 deletions

File tree

Assets/NativeDialogSample.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,41 @@
22
using System.Collections;
33
using NativeDialog;
44

5+
/// <summary>
6+
/// Sample component demonstrating the usage of native dialog functionality.
7+
/// Provides various examples of showing select and submit dialogs with different configurations.
8+
/// </summary>
59
public class NativeDialogSample : MonoBehaviour
610
{
11+
/// <summary>
12+
/// Label text for the positive/confirm button in dialogs.
13+
/// </summary>
714
[SerializeField] private string decideLabel = "Decide";
15+
16+
/// <summary>
17+
/// Label text for the negative/reject button in dialogs.
18+
/// </summary>
819
[SerializeField] private string cancelLabel = "Cancel";
20+
21+
/// <summary>
22+
/// Label text for the close button in submit-only dialogs.
23+
/// </summary>
924
[SerializeField] private string closeLabel = "Close";
1025

26+
/// <summary>
27+
/// Initializes the dialog labels when the component starts.
28+
/// </summary>
1129
private void Start()
1230
{
1331
DialogManager.SetLabel(decideLabel, cancelLabel, closeLabel);
1432
}
1533

1634
#region Invoked from Unity GUI
1735

36+
/// <summary>
37+
/// Shows a simple selection dialog with OK/Cancel buttons.
38+
/// Logs the user's choice to the console.
39+
/// </summary>
1840
public void ShowSelectDialog()
1941
{
2042
const string message = "A simple select dialog";
@@ -24,6 +46,10 @@ public void ShowSelectDialog()
2446
});
2547
}
2648

49+
/// <summary>
50+
/// Shows a selection dialog with both title and message.
51+
/// Useful for providing more context to the user.
52+
/// </summary>
2753
public void ShowSelectDialogWithTitle()
2854
{
2955
const string title = "A title";
@@ -34,6 +60,10 @@ public void ShowSelectDialogWithTitle()
3460
});
3561
}
3662

63+
/// <summary>
64+
/// Shows a submit-only dialog with a single OK button.
65+
/// Used for notifications or acknowledgments.
66+
/// </summary>
3767
public void ShowSubmitDialog()
3868
{
3969
const string message = "A simple submit dialog";
@@ -43,6 +73,10 @@ public void ShowSubmitDialog()
4373
});
4474
}
4575

76+
/// <summary>
77+
/// Shows a submit dialog with both title and message.
78+
/// Provides a more detailed notification to the user.
79+
/// </summary>
4680
public void ShowSubmitDialogWithTitle()
4781
{
4882
const string title = "A title";
@@ -53,6 +87,10 @@ public void ShowSubmitDialogWithTitle()
5387
});
5488
}
5589

90+
/// <summary>
91+
/// Shows a dialog that automatically dismisses after 3 seconds.
92+
/// Demonstrates how to programmatically close dialogs.
93+
/// </summary>
5694
public void ShowDialogWithAutoDismiss()
5795
{
5896
const string message = "A dialog with auto dismiss";

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

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,19 @@
55
namespace NativeDialog
66
{
77
/// <summary>
8-
/// Popup Native Dialog
8+
/// Manages Native Dialog popups across different platforms.
9+
/// Provides a unified interface for showing native select and submit dialogs on iOS, Android, and Unity Editor.
910
/// </summary>
1011
public sealed class DialogManager : MonoBehaviour, IDialogReceiver
1112
{
1213

1314
#region Singleton
1415
private static DialogManager instance;
16+
17+
/// <summary>
18+
/// Gets the singleton instance of DialogManager.
19+
/// Creates a new instance if one doesn't exist.
20+
/// </summary>
1521
public static DialogManager Instance
1622
{
1723
get
@@ -33,10 +39,11 @@ public static DialogManager Instance
3339

3440
#region Members
3541
private Dictionary<int, Action<bool>> callbacks;
42+
3643
private IDialog dialog;
3744
#endregion
3845

39-
#region Lyfecycles
46+
#region Lifecycles
4047
private void Awake()
4148
{
4249
if (instance == null)
@@ -65,9 +72,7 @@ private void Awake()
6572
private IDialog CreateDialog()
6673
{
6774
#if UNITY_EDITOR
68-
var mock = gameObject.AddComponent<DialogMock>();
69-
mock.Initialize(this, true);
70-
return mock;
75+
return new DialogEditor(this);
7176
#elif UNITY_ANDROID
7277
return new DialogAndroid();
7378
#elif UNITY_IOS
@@ -92,39 +97,78 @@ private void OnDestroy()
9297
}
9398
#endregion
9499

100+
#region Public Methods
101+
102+
/// <summary>
103+
/// Sets the button labels for all future dialogs.
104+
/// </summary>
105+
/// <param name="decide">Label for the positive/confirm button</param>
106+
/// <param name="cancel">Label for the negative/cancel button</param>
107+
/// <param name="close">Label for the close button in submit dialogs</param>
95108
public static void SetLabel(string decide, string cancel, string close)
96109
{
97110
Instance.dialog.SetLabel(decide, cancel, close);
98111
}
99112

113+
/// <summary>
114+
/// Shows a selection dialog with OK/Cancel buttons.
115+
/// </summary>
116+
/// <param name="message">The message to display</param>
117+
/// <param name="callback">Callback invoked with true for OK, false for Cancel</param>
118+
/// <returns>Dialog ID that can be used to dismiss the dialog</returns>
100119
public static int ShowSelect(string message, Action<bool> callback)
101120
{
102121
int id = Instance.dialog.ShowSelect(message);
103122
Instance.callbacks.Add(id, callback);
104123
return id;
105124
}
106125

126+
/// <summary>
127+
/// Shows a selection dialog with title and OK/Cancel buttons.
128+
/// </summary>
129+
/// <param name="title">The dialog title</param>
130+
/// <param name="message">The message to display</param>
131+
/// <param name="callback">Callback invoked with true for OK, false for Cancel</param>
132+
/// <returns>Dialog ID that can be used to dismiss the dialog</returns>
107133
public static int ShowSelect(string title, string message, Action<bool> callback)
108134
{
109135
int id = Instance.dialog.ShowSelect(title, message);
110136
Instance.callbacks.Add(id, callback);
111137
return id;
112138
}
113139

140+
/// <summary>
141+
/// Shows a submit dialog with only an OK button.
142+
/// </summary>
143+
/// <param name="message">The message to display</param>
144+
/// <param name="callback">Callback invoked when the dialog is closed</param>
145+
/// <returns>Dialog ID that can be used to dismiss the dialog</returns>
114146
public static int ShowSubmit(string message, Action<bool> callback)
115147
{
116148
int id = Instance.dialog.ShowSubmit(message);
117149
Instance.callbacks.Add(id, callback);
118150
return id;
119151
}
120152

121-
public static int ShowSubmit(string title, string message, Action<bool> del)
153+
/// <summary>
154+
/// Shows a submit dialog with title and only an OK button.
155+
/// </summary>
156+
/// <param name="title">The dialog title</param>
157+
/// <param name="message">The message to display</param>
158+
/// <param name="callback">Callback invoked when the dialog is closed</param>
159+
/// <returns>Dialog ID that can be used to dismiss the dialog</returns>
160+
public static int ShowSubmit(string title, string message, Action<bool> callback)
122161
{
123162
int id = Instance.dialog.ShowSubmit(title, message);
124-
Instance.callbacks.Add(id, del);
163+
Instance.callbacks.Add(id, callback);
125164
return id;
126165
}
127166

167+
/// <summary>
168+
/// Programmatically dismisses a dialog.
169+
/// Invokes the callback with false (cancelled).
170+
/// </summary>
171+
/// <param name="id">The ID of the dialog to dismiss</param>
128172
public static void Dismiss(int id)
129173
{
130174
Instance.dialog.Dismiss(id);
@@ -141,6 +185,7 @@ public static void Dismiss(int id)
141185
}
142186
}
143187

188+
#endregion // Public Methods
144189

145190
#region Invoked from Native Plugin
146191
public void OnSubmit(string idStr)

Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogAndroid.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

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

Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogIOS.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
namespace NativeDialog
66
{
7+
/// <summary>
8+
/// iOS-specific implementation of native dialogs using DllImport to call native methods.
9+
/// </summary>
710
internal sealed class DialogIOS : IDialog
811
{
912
public void Dispose()

0 commit comments

Comments
 (0)