diff --git a/CandyCoded.AlertConfirmDialog.asmdef b/CandyCoded.AlertConfirmDialog.asmdef new file mode 100644 index 0000000..161fcf8 --- /dev/null +++ b/CandyCoded.AlertConfirmDialog.asmdef @@ -0,0 +1,20 @@ +{ + "name": "CandyCoded.AlertConfirmDialog", + "references": [ + "CandyCoded.AlertConfirmDialog.Android", + "CandyCoded.AlertConfirmDialog.iOS" + ], + "includePlatforms": [ + "Android", + "Editor", + "iOS" + ], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/CandyCoded.AlertConfirmDialog.asmdef.meta b/CandyCoded.AlertConfirmDialog.asmdef.meta new file mode 100644 index 0000000..9e325c1 --- /dev/null +++ b/CandyCoded.AlertConfirmDialog.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: ba4a0950d8a4e441bb84796817307f5d +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Plugins.meta b/Plugins.meta new file mode 100644 index 0000000..f1b0540 --- /dev/null +++ b/Plugins.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 391fec1ddb7c945d8abf97096e6ace18 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Plugins/Android.meta b/Plugins/Android.meta new file mode 100644 index 0000000..7ff5103 --- /dev/null +++ b/Plugins/Android.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: c0db598e993614f4a915d2f8b8ec42b7 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Plugins/Android/AlertConfirmDialog.cs b/Plugins/Android/AlertConfirmDialog.cs new file mode 100644 index 0000000..629451e --- /dev/null +++ b/Plugins/Android/AlertConfirmDialog.cs @@ -0,0 +1,46 @@ +// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. + +using UnityEngine; + +namespace CandyCoded.AlertConfirmDialog.Android +{ + + public static class AlertConfirmDialog + { + + private static AndroidJavaObject _androidPlugin; + + private static AndroidJavaObject androidPlugin + { + get + { + if (_androidPlugin != null) + { + return _androidPlugin; + } + + var javaUnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); + + var currentActivity = javaUnityPlayer.GetStatic("currentActivity"); + + _androidPlugin = + new AndroidJavaObject("com.candycoded.alertconfirmdialog.AndroidPlugin", currentActivity); + + return _androidPlugin; + } + } + + public static void Alert(string title, string message, string okButtonLabel) + { + androidPlugin.Call("Alert", title, message, okButtonLabel); + } + + public static void Confirm(string title, string message, string okButtonLabel, + string cancelButtonLabel) + { + androidPlugin.Call("Confirm", title, message, okButtonLabel, cancelButtonLabel); + } + + } + +} diff --git a/Plugins/Android/AlertConfirmDialog.cs.meta b/Plugins/Android/AlertConfirmDialog.cs.meta new file mode 100644 index 0000000..aa72cf2 --- /dev/null +++ b/Plugins/Android/AlertConfirmDialog.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 387570baaf6540fb92c6108ee81516ea +timeCreated: 1613539850 \ No newline at end of file diff --git a/Plugins/Android/AndroidPlugin.java b/Plugins/Android/AndroidPlugin.java new file mode 100644 index 0000000..2363670 --- /dev/null +++ b/Plugins/Android/AndroidPlugin.java @@ -0,0 +1,62 @@ +// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. + +package com.candycoded.alertconfirmdialog; + +import android.content.Context; +import android.app.AlertDialog; +import android.os.Bundle; + +import com.unity3d.player.UnityPlayer; + +public class AndroidPlugin { + + private Context context; + + public AndroidPlugin(Context context) { + this.context = context; + } + + public void Alert(String title, String message, String okButtonLabel) { + + AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); + + alertDialog.setTitle(title); + alertDialog.setMessage(message); + alertDialog.setPositiveButton(okButtonLabel, (dialog, which) -> { + + dialog.dismiss(); + + UnityPlayer.UnitySendMessage("AlertConfirmDialog", "Callback", "OK"); + + }); + alertDialog.create(); + alertDialog.show(); + + } + + public void Confirm(String title, String message, String okButtonLabel, String cancelButtonLabel) { + + AlertDialog.Builder alertDialog = new AlertDialog.Builder(context); + + alertDialog.setTitle(title); + alertDialog.setMessage(message); + alertDialog.setPositiveButton(okButtonLabel, (dialog, which) -> { + + dialog.dismiss(); + + UnityPlayer.UnitySendMessage("AlertConfirmDialog", "Callback", "OK"); + + }); + alertDialog.setNegativeButton(cancelButtonLabel, (dialog, which) -> { + + dialog.dismiss(); + + UnityPlayer.UnitySendMessage("AlertConfirmDialog", "Callback", "CANCEL"); + + }); + alertDialog.create(); + alertDialog.show(); + + } + +} diff --git a/Plugins/Android/AndroidPlugin.java.meta b/Plugins/Android/AndroidPlugin.java.meta new file mode 100644 index 0000000..051103e --- /dev/null +++ b/Plugins/Android/AndroidPlugin.java.meta @@ -0,0 +1,81 @@ +fileFormatVersion: 2 +guid: 290b0c84318f14ef193b5a8a24125f62 +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + : Any + second: + enabled: 0 + settings: + Exclude Android: 1 + Exclude Editor: 1 + Exclude Linux64: 1 + Exclude OSXUniversal: 1 + Exclude WebGL: 1 + Exclude Win: 1 + Exclude Win64: 1 + Exclude iOS: 1 + - first: + Android: Android + second: + enabled: 0 + settings: + CPU: ARMv7 + - first: + Any: + second: + enabled: 0 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + CPU: AnyCPU + DefaultValueInitialized: true + OS: AnyOS + - first: + Standalone: Linux64 + second: + enabled: 0 + settings: + CPU: x86_64 + - first: + Standalone: OSXUniversal + second: + enabled: 0 + settings: + CPU: x86_64 + - first: + Standalone: Win + second: + enabled: 0 + settings: + CPU: x86 + - first: + Standalone: Win64 + second: + enabled: 0 + settings: + CPU: x86_64 + - first: + iPhone: iOS + second: + enabled: 0 + settings: + AddToEmbeddedBinaries: false + CPU: AnyCPU + CompileFlags: + FrameworkDependencies: + userData: + assetBundleName: + assetBundleVariant: diff --git a/Plugins/Android/CandyCoded.AlertConfirmDialog.Android.asmdef b/Plugins/Android/CandyCoded.AlertConfirmDialog.Android.asmdef new file mode 100644 index 0000000..d5c173e --- /dev/null +++ b/Plugins/Android/CandyCoded.AlertConfirmDialog.Android.asmdef @@ -0,0 +1,16 @@ +{ + "name": "CandyCoded.AlertConfirmDialog.Android", + "references": [], + "includePlatforms": [ + "Android", + "Editor" + ], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Plugins/Android/CandyCoded.AlertConfirmDialog.Android.asmdef.meta b/Plugins/Android/CandyCoded.AlertConfirmDialog.Android.asmdef.meta new file mode 100644 index 0000000..35ca21e --- /dev/null +++ b/Plugins/Android/CandyCoded.AlertConfirmDialog.Android.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 9ec7790458237408d96f3429328c7c8b +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Plugins/Android/androidplugin-release.aar b/Plugins/Android/androidplugin-release.aar new file mode 100644 index 0000000..716781b Binary files /dev/null and b/Plugins/Android/androidplugin-release.aar differ diff --git a/Plugins/Android/androidplugin-release.aar.meta b/Plugins/Android/androidplugin-release.aar.meta new file mode 100644 index 0000000..72ab261 --- /dev/null +++ b/Plugins/Android/androidplugin-release.aar.meta @@ -0,0 +1,32 @@ +fileFormatVersion: 2 +guid: cf01909b86535413380ec48048b7ab98 +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Android: Android + second: + enabled: 1 + settings: {} + - first: + Any: + second: + enabled: 0 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + userData: + assetBundleName: + assetBundleVariant: diff --git a/Plugins/iOS.meta b/Plugins/iOS.meta new file mode 100644 index 0000000..3d245e9 --- /dev/null +++ b/Plugins/iOS.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d35f73331e19041ec980dee228d02712 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Plugins/iOS/AlertConfirmDialog.cs b/Plugins/iOS/AlertConfirmDialog.cs new file mode 100644 index 0000000..9563557 --- /dev/null +++ b/Plugins/iOS/AlertConfirmDialog.cs @@ -0,0 +1,20 @@ +// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. + +using System.Runtime.InteropServices; + +namespace CandyCoded.AlertConfirmDialog.iOS +{ + + public static class AlertConfirmDialog + { + + [DllImport("__Internal")] + public static extern void Alert(string title, string message, string okButtonLabel); + + [DllImport("__Internal")] + public static extern void Confirm(string title, string message, string okButtonLabel, + string cancelButtonLabel); + + } + +} diff --git a/Plugins/iOS/AlertConfirmDialog.cs.meta b/Plugins/iOS/AlertConfirmDialog.cs.meta new file mode 100644 index 0000000..22e1a9f --- /dev/null +++ b/Plugins/iOS/AlertConfirmDialog.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 42a29b7453f034ef6aa7df6619c4730f +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Plugins/iOS/AlertConfirmDialog.mm b/Plugins/iOS/AlertConfirmDialog.mm new file mode 100644 index 0000000..bddd9ab --- /dev/null +++ b/Plugins/iOS/AlertConfirmDialog.mm @@ -0,0 +1,70 @@ +// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. + +#import +#import +#import + +extern "C" +{ + + void Alert(const char* title, const char* message, const char* okButtonLabel) { + + NSString* titleString = [NSString stringWithUTF8String: title]; + NSString* messageString = [NSString stringWithUTF8String: message]; + + NSString* okButtonLabelString = [NSString stringWithUTF8String: okButtonLabel]; + + UIAlertController* alert = [UIAlertController alertControllerWithTitle:titleString + message:messageString + preferredStyle:UIAlertControllerStyleAlert]; + + UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:okButtonLabelString style:UIAlertActionStyleDefault + handler:^(UIAlertAction * action) { + + UnitySendMessage("AlertConfirmDialog", "Callback", "OK"); + + }]; + [alert addAction:defaultAction]; + + UIViewController* view = [[[UIApplication sharedApplication] keyWindow] rootViewController]; + + [view presentViewController:alert animated:YES completion:nil]; + + } + + void Confirm(const char* title, const char* message, const char* okButtonLabel, const char* cancelButtonLabel) { + + NSString* titleString = [NSString stringWithUTF8String: title]; + NSString* messageString = [NSString stringWithUTF8String: message]; + + NSString* okButtonLabelString = [NSString stringWithUTF8String: okButtonLabel]; + NSString* cancelButtonLabelString = [NSString stringWithUTF8String: cancelButtonLabel]; + + UIAlertController* alert = [UIAlertController alertControllerWithTitle:titleString + message:messageString + preferredStyle:UIAlertControllerStyleAlert]; + + UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:okButtonLabelString style:UIAlertActionStyleDefault + handler:^(UIAlertAction * action) { + + UnitySendMessage("AlertConfirmDialog", "Callback", "OK"); + + }]; + [alert addAction:defaultAction]; + + UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:cancelButtonLabelString style:UIAlertActionStyleCancel + handler:^(UIAlertAction * action) { + + UnitySendMessage("AlertConfirmDialog", "Callback", "CANCEL"); + + }]; + [alert addAction:cancelAction]; + [alert setPreferredAction:defaultAction]; + + UIViewController* view = [[[UIApplication sharedApplication] keyWindow] rootViewController]; + + [view presentViewController:alert animated:YES completion:nil]; + + } + +} \ No newline at end of file diff --git a/Plugins/iOS/AlertConfirmDialog.mm.meta b/Plugins/iOS/AlertConfirmDialog.mm.meta new file mode 100644 index 0000000..0204a7f --- /dev/null +++ b/Plugins/iOS/AlertConfirmDialog.mm.meta @@ -0,0 +1,37 @@ +fileFormatVersion: 2 +guid: 349b383cb981443959172fb7c8c4391a +PluginImporter: + externalObjects: {} + serializedVersion: 2 + iconMap: {} + executionOrder: {} + defineConstraints: [] + isPreloaded: 0 + isOverridable: 0 + isExplicitlyReferenced: 0 + validateReferences: 1 + platformData: + - first: + Any: + second: + enabled: 0 + settings: {} + - first: + Editor: Editor + second: + enabled: 0 + settings: + DefaultValueInitialized: true + - first: + iPhone: iOS + second: + enabled: 1 + settings: {} + - first: + tvOS: tvOS + second: + enabled: 1 + settings: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Plugins/iOS/CandyCoded.AlertConfirmDialog.iOS.asmdef b/Plugins/iOS/CandyCoded.AlertConfirmDialog.iOS.asmdef new file mode 100644 index 0000000..6dca854 --- /dev/null +++ b/Plugins/iOS/CandyCoded.AlertConfirmDialog.iOS.asmdef @@ -0,0 +1,16 @@ +{ + "name": "CandyCoded.AlertConfirmDialog.iOS", + "references": [], + "includePlatforms": [ + "Editor", + "iOS" + ], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Plugins/iOS/CandyCoded.AlertConfirmDialog.iOS.asmdef.meta b/Plugins/iOS/CandyCoded.AlertConfirmDialog.iOS.asmdef.meta new file mode 100644 index 0000000..114100f --- /dev/null +++ b/Plugins/iOS/CandyCoded.AlertConfirmDialog.iOS.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 870c9fb6fd6b644b3bbd45a5490dd4b2 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts.meta b/Scripts.meta new file mode 100644 index 0000000..9b5c20c --- /dev/null +++ b/Scripts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: b317ee7df2d43463ba3b296fe1144513 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/AlertConfirmDialog.cs b/Scripts/AlertConfirmDialog.cs new file mode 100644 index 0000000..7480f77 --- /dev/null +++ b/Scripts/AlertConfirmDialog.cs @@ -0,0 +1,65 @@ +// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. + +using System; +using UnityEngine; + +namespace CandyCoded.AlertConfirmDialog +{ + + public static class AlertConfirmDialog + { + + private static UnitySendMessageEventHandler _unitySendMessageEventHandler; + + public static void Alert(string title, string message, Action okButtonCallback = null, + string okButtonLabel = "OK") + { + + if (_unitySendMessageEventHandler == null) + { + + var gameObject = new GameObject("AlertConfirmDialog", typeof(UnitySendMessageEventHandler)); + + gameObject.TryGetComponent(out _unitySendMessageEventHandler); + + } + + _unitySendMessageEventHandler.ok = okButtonCallback; + +#if UNITY_IOS && !UNITY_EDITOR + iOS.AlertConfirmDialog.Alert(title, message, okButtonLabel); +#elif UNITY_ANDROID && !UNITY_EDITOR + Android.AlertConfirmDialog.Alert(title, message, okButtonLabel); +#endif + + } + + public static void Confirm(string title, string message, Action okButtonCallback = null, + Action cancelButtonCallback = null, + string okButtonLabel = "OK", string cancelButtonLabel = "Cancel") + { + + if (_unitySendMessageEventHandler == null) + { + + var gameObject = new GameObject("AlertConfirmDialog", typeof(UnitySendMessageEventHandler)); + + gameObject.TryGetComponent(out _unitySendMessageEventHandler); + + } + + _unitySendMessageEventHandler.ok = okButtonCallback; + + _unitySendMessageEventHandler.cancel = cancelButtonCallback; + +#if UNITY_IOS && !UNITY_EDITOR + iOS.AlertConfirmDialog.Confirm(title, message, okButtonLabel, cancelButtonLabel); +#elif UNITY_ANDROID && !UNITY_EDITOR + Android.AlertConfirmDialog.Confirm(title, message, okButtonLabel, cancelButtonLabel); +#endif + + } + + } + +} diff --git a/Scripts/AlertConfirmDialog.cs.meta b/Scripts/AlertConfirmDialog.cs.meta new file mode 100644 index 0000000..280c583 --- /dev/null +++ b/Scripts/AlertConfirmDialog.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: c6bc1d72e295849a0a72158c68c56d97 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/UnitySendMessageEventHandler.cs b/Scripts/UnitySendMessageEventHandler.cs new file mode 100644 index 0000000..9cd8af8 --- /dev/null +++ b/Scripts/UnitySendMessageEventHandler.cs @@ -0,0 +1,39 @@ +// Copyright (c) Scott Doxey. All Rights Reserved. Licensed under the MIT License. See LICENSE in the project root for license information. + +using System; +using UnityEngine; + +namespace CandyCoded.AlertConfirmDialog +{ + + public class UnitySendMessageEventHandler : MonoBehaviour + { + + public Action cancel; + + public Action ok; + + public void Callback(string message) + { + + switch (message) + { + case "OK": + ok?.Invoke(); + + break; + case "CANCEL": + cancel?.Invoke(); + + break; + } + + ok = null; + + cancel = null; + + } + + } + +} diff --git a/Scripts/UnitySendMessageEventHandler.cs.meta b/Scripts/UnitySendMessageEventHandler.cs.meta new file mode 100644 index 0000000..806bb2e --- /dev/null +++ b/Scripts/UnitySendMessageEventHandler.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 651dc4d7dda3d4f7891de602a135a9e4 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/package.json b/package.json new file mode 100644 index 0000000..1dd1ff0 --- /dev/null +++ b/package.json @@ -0,0 +1,29 @@ +{ + "name": "xyz.candycoded.alertconfirmdialog", + "displayName": "Alert Confirm Dialog", + "version": "1.0.0", + "unity": "2019.2", + "unityRelease": "0f1", + "description": "Display simple alert and confirm dialogs on both iOS and Android.", + "license": "MIT", + "dependencies": { + "com.unity.modules.androidjni": "1.0.0" + }, + "keywords": [ + "ios", + "android", + "alert", + "confirm", + "dialog" + ], + "author": { + "name": "Scott Doxey", + "email": "hello@scottdoxey.com", + "homepage": "http://scottdoxey.com/" + }, + "homepage": "https://github.com/CandyCoded/AlertConfirmDialog", + "repository": { + "type": "git", + "url": "git@github.com:CandyCoded/AlertConfirmDialog.git" + } +} diff --git a/package.json.meta b/package.json.meta new file mode 100644 index 0000000..4146e57 --- /dev/null +++ b/package.json.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 9b3eb19685cc3424ea37bada7c852fd6 +TextScriptImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: