diff --git a/Assets/NativeDialogSample.cs b/Assets/NativeDialogSample.cs index c5a4cdb..038c285 100644 --- a/Assets/NativeDialogSample.cs +++ b/Assets/NativeDialogSample.cs @@ -2,12 +2,30 @@ using System.Collections; using NativeDialog; +/// +/// Sample component demonstrating the usage of native dialog functionality. +/// Provides various examples of showing select and submit dialogs with different configurations. +/// public class NativeDialogSample : MonoBehaviour { + /// + /// Label text for the positive/confirm button in dialogs. + /// [SerializeField] private string decideLabel = "Decide"; + + /// + /// Label text for the negative/reject button in dialogs. + /// [SerializeField] private string cancelLabel = "Cancel"; + + /// + /// Label text for the close button in submit-only dialogs. + /// [SerializeField] private string closeLabel = "Close"; + /// + /// Initializes the dialog labels when the component starts. + /// private void Start() { DialogManager.SetLabel(decideLabel, cancelLabel, closeLabel); @@ -15,6 +33,10 @@ private void Start() #region Invoked from Unity GUI + /// + /// Shows a simple selection dialog with OK/Cancel buttons. + /// Logs the user's choice to the console. + /// public void ShowSelectDialog() { const string message = "A simple select dialog"; @@ -24,6 +46,10 @@ public void ShowSelectDialog() }); } + /// + /// Shows a selection dialog with both title and message. + /// Useful for providing more context to the user. + /// public void ShowSelectDialogWithTitle() { const string title = "A title"; @@ -34,6 +60,10 @@ public void ShowSelectDialogWithTitle() }); } + /// + /// Shows a submit-only dialog with a single OK button. + /// Used for notifications or acknowledgments. + /// public void ShowSubmitDialog() { const string message = "A simple submit dialog"; @@ -43,6 +73,10 @@ public void ShowSubmitDialog() }); } + /// + /// Shows a submit dialog with both title and message. + /// Provides a more detailed notification to the user. + /// public void ShowSubmitDialogWithTitle() { const string title = "A title"; @@ -53,6 +87,10 @@ public void ShowSubmitDialogWithTitle() }); } + /// + /// Shows a dialog that automatically dismisses after 3 seconds. + /// Demonstrates how to programmatically close dialogs. + /// public void ShowDialogWithAutoDismiss() { const string message = "A dialog with auto dismiss"; diff --git a/Packages/com.github.asus4.nativedialog/Runtime/DialogManager.cs b/Packages/com.github.asus4.nativedialog/Runtime/DialogManager.cs index c84951d..42c6f39 100644 --- a/Packages/com.github.asus4.nativedialog/Runtime/DialogManager.cs +++ b/Packages/com.github.asus4.nativedialog/Runtime/DialogManager.cs @@ -5,13 +5,19 @@ namespace NativeDialog { /// - /// Popup Native Dialog + /// Manages Native Dialog popups across different platforms. + /// Provides a unified interface for showing native select and submit dialogs on iOS, Android, and Unity Editor. /// public sealed class DialogManager : MonoBehaviour, IDialogReceiver { #region Singleton private static DialogManager instance; + + /// + /// Gets the singleton instance of DialogManager. + /// Creates a new instance if one doesn't exist. + /// public static DialogManager Instance { get @@ -33,10 +39,11 @@ public static DialogManager Instance #region Members private Dictionary> callbacks; + private IDialog dialog; #endregion - #region Lyfecycles + #region Lifecycles private void Awake() { if (instance == null) @@ -65,9 +72,7 @@ private void Awake() private IDialog CreateDialog() { #if UNITY_EDITOR - var mock = gameObject.AddComponent(); - mock.Initialize(this, true); - return mock; + return new DialogEditor(this); #elif UNITY_ANDROID return new DialogAndroid(); #elif UNITY_IOS @@ -92,11 +97,25 @@ private void OnDestroy() } #endregion + #region Public Methods + + /// + /// Sets the button labels for all future dialogs. + /// + /// Label for the positive/confirm button + /// Label for the negative/cancel button + /// Label for the close button in submit dialogs public static void SetLabel(string decide, string cancel, string close) { Instance.dialog.SetLabel(decide, cancel, close); } + /// + /// Shows a selection dialog with OK/Cancel buttons. + /// + /// The message to display + /// Callback invoked with true for OK, false for Cancel + /// Dialog ID that can be used to dismiss the dialog public static int ShowSelect(string message, Action callback) { int id = Instance.dialog.ShowSelect(message); @@ -104,6 +123,13 @@ public static int ShowSelect(string message, Action callback) return id; } + /// + /// Shows a selection dialog with title and OK/Cancel buttons. + /// + /// The dialog title + /// The message to display + /// Callback invoked with true for OK, false for Cancel + /// Dialog ID that can be used to dismiss the dialog public static int ShowSelect(string title, string message, Action callback) { int id = Instance.dialog.ShowSelect(title, message); @@ -111,6 +137,12 @@ public static int ShowSelect(string title, string message, Action callback return id; } + /// + /// Shows a submit dialog with only an OK button. + /// + /// The message to display + /// Callback invoked when the dialog is closed + /// Dialog ID that can be used to dismiss the dialog public static int ShowSubmit(string message, Action callback) { int id = Instance.dialog.ShowSubmit(message); @@ -118,13 +150,25 @@ public static int ShowSubmit(string message, Action callback) return id; } - public static int ShowSubmit(string title, string message, Action del) + /// + /// Shows a submit dialog with title and only an OK button. + /// + /// The dialog title + /// The message to display + /// Callback invoked when the dialog is closed + /// Dialog ID that can be used to dismiss the dialog + public static int ShowSubmit(string title, string message, Action callback) { int id = Instance.dialog.ShowSubmit(title, message); - Instance.callbacks.Add(id, del); + Instance.callbacks.Add(id, callback); return id; } + /// + /// Programmatically dismisses a dialog. + /// Invokes the callback with false (cancelled). + /// + /// The ID of the dialog to dismiss public static void Dismiss(int id) { Instance.dialog.Dismiss(id); @@ -141,6 +185,7 @@ public static void Dismiss(int id) } } + #endregion // Public Methods #region Invoked from Native Plugin public void OnSubmit(string idStr) diff --git a/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogAndroid.cs b/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogAndroid.cs index 1c68ccc..b3a909c 100644 --- a/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogAndroid.cs +++ b/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogAndroid.cs @@ -4,6 +4,9 @@ namespace NativeDialog { + /// + /// Android-specific implementation of native dialogs using AndroidJavaClass. + /// internal sealed class DialogAndroid : IDialog { private readonly AndroidJavaClass cls; diff --git a/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogEditor.cs b/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogEditor.cs new file mode 100644 index 0000000..d6a2be1 --- /dev/null +++ b/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogEditor.cs @@ -0,0 +1,137 @@ +#if UNITY_EDITOR +using System.Collections.Generic; +using UnityEditor; + +namespace NativeDialog +{ + /// + /// Mock implementation of dialogs for Unity Editor. + /// + internal sealed class DialogEditor : IDialog + { + private string decideLabel = "YES"; + private string cancelLabel = "NO"; + private string closeLabel = "CLOSE"; + private int currentId = 0; + private IDialogReceiver receiver; + private readonly Dictionary pendingDialogs = new Dictionary(); + + public DialogEditor(IDialogReceiver receiver) + { + this.receiver = receiver; + } + + public void Dispose() + { + pendingDialogs.Clear(); + receiver = null; + } + + public void SetLabel(string decide, string cancel, string close) + { + decideLabel = decide; + cancelLabel = cancel; + closeLabel = close; + } + + public int ShowSelect(string message) + { + int id = ++currentId; + EditorApplication.delayCall += () => ShowSelectDialog(id, null, message); + return id; + } + + public int ShowSelect(string title, string message) + { + int id = ++currentId; + EditorApplication.delayCall += () => ShowSelectDialog(id, title, message); + return id; + } + + public int ShowSubmit(string message) + { + int id = ++currentId; + EditorApplication.delayCall += () => ShowSubmitDialog(id, null, message); + return id; + } + + public int ShowSubmit(string title, string message) + { + int id = ++currentId; + EditorApplication.delayCall += () => ShowSubmitDialog(id, title, message); + return id; + } + + public void Dismiss(int id) + { + UnityEngine.Debug.LogWarning($"Dismiss is not supported in Editor mode. ID: {id}"); + if (pendingDialogs.ContainsKey(id)) + { + pendingDialogs.Remove(id); + } + } + + private void ShowSelectDialog(int id, string title, string message) + { + if (!pendingDialogs.ContainsKey(id)) + { + pendingDialogs[id] = true; + } + else if (!pendingDialogs[id]) + { + return; + } + + bool result; + if (string.IsNullOrEmpty(title)) + { + result = EditorUtility.DisplayDialog("", message, decideLabel, cancelLabel); + } + else + { + result = EditorUtility.DisplayDialog(title, message, decideLabel, cancelLabel); + } + + if (pendingDialogs.ContainsKey(id)) + { + pendingDialogs.Remove(id); + if (result) + { + receiver?.OnSubmit(id.ToString()); + } + else + { + receiver?.OnCancel(id.ToString()); + } + } + } + + private void ShowSubmitDialog(int id, string title, string message) + { + if (!pendingDialogs.ContainsKey(id)) + { + pendingDialogs[id] = true; + } + else if (!pendingDialogs[id]) + { + return; + } + + if (string.IsNullOrEmpty(title)) + { + EditorUtility.DisplayDialog("", message, closeLabel); + } + else + { + EditorUtility.DisplayDialog(title, message, closeLabel); + } + + if (pendingDialogs.ContainsKey(id)) + { + pendingDialogs.Remove(id); + receiver?.OnSubmit(id.ToString()); + } + } + } +} +#endif diff --git a/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogEditor.cs.meta b/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogEditor.cs.meta new file mode 100644 index 0000000..6e52e90 --- /dev/null +++ b/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogEditor.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 9a8b5c4d6e7f8423490b1234567890ab +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: \ No newline at end of file diff --git a/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogIOS.cs b/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogIOS.cs index 61fe848..c670dda 100644 --- a/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogIOS.cs +++ b/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogIOS.cs @@ -4,6 +4,9 @@ namespace NativeDialog { + /// + /// iOS-specific implementation of native dialogs using DllImport to call native methods. + /// internal sealed class DialogIOS : IDialog { public void Dispose() diff --git a/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogMock.cs b/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogMock.cs index 58cda6d..9d23566 100644 --- a/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogMock.cs +++ b/Packages/com.github.asus4.nativedialog/Runtime/Internal/DialogMock.cs @@ -3,6 +3,10 @@ namespace NativeDialog { + /// + /// Mock implementation of dialogs for Unity Editor testing. + /// Simulates dialog behavior with configurable delays and results. + /// internal sealed class DialogMock : MonoBehaviour, IDialog { [SerializeField] diff --git a/Packages/com.github.asus4.nativedialog/Runtime/Internal/IDialog.cs b/Packages/com.github.asus4.nativedialog/Runtime/Internal/IDialog.cs index ef0d481..7dc213b 100644 --- a/Packages/com.github.asus4.nativedialog/Runtime/Internal/IDialog.cs +++ b/Packages/com.github.asus4.nativedialog/Runtime/Internal/IDialog.cs @@ -1,5 +1,9 @@ namespace NativeDialog { + /// + /// Interface for platform-specific dialog implementations. + /// Defines methods for showing native dialogs across different platforms. + /// internal interface IDialog : System.IDisposable { void SetLabel(string decide, string cancel, string close); diff --git a/Packages/com.github.asus4.nativedialog/Runtime/Internal/IDialogReceiver.cs b/Packages/com.github.asus4.nativedialog/Runtime/Internal/IDialogReceiver.cs index e8b5ba2..f5daa88 100644 --- a/Packages/com.github.asus4.nativedialog/Runtime/Internal/IDialogReceiver.cs +++ b/Packages/com.github.asus4.nativedialog/Runtime/Internal/IDialogReceiver.cs @@ -1,5 +1,9 @@ namespace NativeDialog { + /// + /// Interface for receiving callbacks from native dialog implementations. + /// Handles user interactions with dialog buttons. + /// internal interface IDialogReceiver { void OnSubmit(string idStr);