|
| 1 | +# Unity Android SDK Plugin Permission Helper |
| 2 | + |
| 3 | +This unity android plugin provides android permission helper to check permission (granted or not) and allow you to ask android permission/permissions for your Unity project. |
| 4 | + |
| 5 | +## Plugin Functions/Features : |
| 6 | + |
| 7 | +- Check if a specific permission is already granted to your app or not. |
| 8 | +- Allow you to request single/multiple permissions from user. |
| 9 | +- Callbacks can be used to receive permission granted or not with status only or with full information. |
| 10 | + |
| 11 | +## Plugin Usage : |
| 12 | + |
| 13 | +In your unity project, you can use this plugins in your C# scripts. |
| 14 | +Download the plugin .aar file (pluginpermissionhelper-debug.aar or pluginpermissionhelper-release.aar based on development environment) from releases or |
| 15 | +build it by yourself and paste the .aar file in your Unity project's `Assets/Plugins/Android` directory. (Follow main README.md for plugin setup info) |
| 16 | + |
| 17 | +### Common code required to access all functions |
| 18 | + |
| 19 | +``` |
| 20 | + const string pluginName = "com.arupakaman.pluginpermissionhelper.ui.RequestPermissionActivity"; // Constant of RequestPermissionActivity class location |
| 21 | +
|
| 22 | + AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); |
| 23 | + AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity"); // jo will be used for Context param |
| 24 | + AndroidJavaClass ajcPermissionHelper = new AndroidJavaClass(pluginName); // ajcWebView will be used to access WebViewActivity class |
| 25 | +``` |
| 26 | +### To Check if Permission is Granted or not |
| 27 | + |
| 28 | +Below function returns true if permission is already granted. |
| 29 | + |
| 30 | +``` |
| 31 | + bool isGranted = ajcPermissionHelper.CallStatic<bool>( |
| 32 | + "hasPermission", // function name to check permission |
| 33 | + jo, // first param context defined above |
| 34 | + "android.permission.ACCESS_FINE_LOCATION", // Permission name constant String [see more](https://developer.android.com/reference/android/Manifest.permission#constants) |
| 35 | + ); |
| 36 | +
|
| 37 | +``` |
| 38 | + |
| 39 | +### To Request Permission/Permissions |
| 40 | + |
| 41 | +``` |
| 42 | + // Note* : below variable jsonPermissionData is not in correct String format, make sure you create your json in String |
| 43 | + String jsonPermissionData = { |
| 44 | + "obj_name": "Name of the Object the Unity Script is attached to", |
| 45 | + "unity_status_method_name": "methodToReceivePermissionStatus with a String param", |
| 46 | + "unity_info_method_name": "methodToReceivePermissionDetailedInfo with a String param", // you can also use one of the callback method unity_status_method_name or unity_info_method_name |
| 47 | + "handle_mandatory": "true/false to handle mandatory permission dialog in plugin (Optional, by default true)", |
| 48 | + "handle_rational": "true/false to handle mandatory permission dialog in plugin (Optional, by default true)", |
| 49 | + "permission_json_data": [ |
| 50 | + { |
| 51 | + "perm_name": "android.permission.ACCESS_FINE_LOCATION", //Use Constant values [from here](https://developer.android.com/reference/android/Manifest.permission#constants) |
| 52 | + "perm_visible_name": "Location Permission", // Name that will be visible to user in mandatory/rational dialogs |
| 53 | + "is_mandatory": "true/false // true if permission is mandatory (Optional, by default true)" |
| 54 | + } |
| 55 | + ] |
| 56 | + } |
| 57 | +
|
| 58 | + ajcPermissionHelper.CallStatic( |
| 59 | + "startRequestPermissionActivity", // function name to launch Permission request Activity |
| 60 | + jo, // first param context defined above |
| 61 | + jsonPermissionData // json as String with permission and callback information |
| 62 | + ); |
| 63 | +
|
| 64 | +
|
| 65 | + // A valid jsonPermissionData variable example: |
| 66 | + String jsonPermissionData = "{\"obj_name\": \"Text\", \"unity_status_method_name\": \"PermissionGrantedStatus\", \"unity_info_method_name\": \"PermissionGrantedInfo\", \"handle_mandatory\": \"true\", \"handle_rational\": \"true\",\"permission_json_data\": [{\"perm_name\": \"android.permission.ACCESS_FINE_LOCATION\", \"perm_visible_name\": \"Location Permission\", \"is_mandatory\": \"true\"}]}" |
| 67 | +
|
| 68 | +``` |
| 69 | + |
| 70 | +### To receive permission request status callback |
| 71 | + |
| 72 | +You can use one of the below methods or both of them : |
| 73 | + |
| 74 | +1) You can implement a method with a String parameter to receive the overall permission status. This method will receive status in the String parameter with one of the below values : |
| 75 | + GRANTED (If user has granted all the mandatory permissions) |
| 76 | + NOT_GRANTED (If user has not granted any one of the mandatory permissions) |
| 77 | + SHOW_RATIONAL (If user has selected not to ask again for a mandatory permission) |
| 78 | + |
| 79 | +Status response example = {"status":"GRANTED"} |
| 80 | +Send this method name in json key = "unity_status_method_name" |
| 81 | + |
| 82 | +2) You can implement a method with a String parameter to receive the complete permission status information. This method will receive a json object with status of all requested permissions and other data as follows: |
| 83 | + |
| 84 | +Example response = [{"perm_name":"android.permission.ACCESS_FINE_LOCATION","perm_visible_name":"Location Permission","is_mandatory":true,"is_granted":true}] |
| 85 | +Send this method name in json key = "unity_info_method_name" |
| 86 | + |
| 87 | +You can parse the response and use the keys to check which permission is granted and which is not. |
| 88 | + |
| 89 | +### Customize dialog Strings |
| 90 | + |
| 91 | +Override the below string resource in your Unity project to customize the error message : |
| 92 | + |
| 93 | +``` |
| 94 | + <!-- Override these strings in your Unity project to customize the dialog text --> |
| 95 | +
|
| 96 | + <string name="dialog_message_rational">You need to grant required permission from device settings :</string> |
| 97 | + <string name="dialog_action_rational_positive">Grant Now</string> |
| 98 | + <string name="dialog_action_rational_negative">Not Now</string> |
| 99 | + <string name="dialog_message_perm_mandatory">You need to grant required permission :</string> |
| 100 | + <string name="dialog_action_perm_mandatory">Okay</string> |
| 101 | +``` |
| 102 | + |
| 103 | +### I prefer a :star: than a cup of :coffee: |
0 commit comments