Skip to content

Commit 3e856f2

Browse files
committed
Initial commit
0 parents  commit 3e856f2

11 files changed

Lines changed: 241 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.meta
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.company.productname">
3+
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:smallScreens="true" android:xlargeScreens="true" />
4+
<application android:icon="@drawable/app_icon" android:label="@string/app_name" android:theme="@style/UnityThemeSelector">
5+
<activity android:label="@string/app_name" android:name="com.unity3d.player.UnityPlayerActivity">
6+
<intent-filter>
7+
<action android:name="android.intent.action.MAIN" />
8+
<category android:name="android.intent.category.LAUNCHER" />
9+
<category android:name="android.intent.category.LEANBACK_LAUNCHER" />
10+
</intent-filter>
11+
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
12+
<meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />
13+
</activity>
14+
</application>
15+
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="25" />
16+
</manifest>
16.6 KB
Binary file not shown.
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using UnityEngine;
2+
3+
public class AndroidPermissionCallback : AndroidJavaProxy
4+
{
5+
public AndroidPermissionCallback() : base("com.unity3d.player.UnityAndroidPermissions$IPermissionRequestResult") { }
6+
7+
// Handle permission granted
8+
public virtual void OnPermissionGranted(string permissionName)
9+
{
10+
//Debug.Log("Permission " + permissionName + " GRANTED");
11+
}
12+
13+
// Handle permission denied
14+
public virtual void OnPermissionDenied(string permissionName)
15+
{
16+
//Debug.Log("Permission " + permissionName + " DENIED!");
17+
}
18+
}
19+
20+
public class AndroidPermissionsManager
21+
{
22+
private static AndroidJavaObject m_Activity;
23+
private static AndroidJavaObject m_PermissionService;
24+
25+
private static AndroidJavaObject GetActivity()
26+
{
27+
if (m_Activity == null)
28+
{
29+
AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
30+
m_Activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
31+
}
32+
return m_Activity;
33+
}
34+
35+
private static AndroidJavaObject GetPermissionsService()
36+
{
37+
if (m_PermissionService == null)
38+
{
39+
m_PermissionService = new AndroidJavaObject("com.unity3d.player.UnityAndroidPermissions");
40+
}
41+
return m_PermissionService;
42+
}
43+
44+
45+
public static bool IsPermissionGranted(string permissionName)
46+
{
47+
return GetPermissionsService().Call<bool>("IsPermissionGranted", GetActivity(), permissionName);
48+
}
49+
50+
public static void RequestPermission(string[] permissionNames, AndroidPermissionCallback callback)
51+
{
52+
GetPermissionsService().Call("RequestPermissionAsync", GetActivity(), permissionNames, callback);
53+
}
54+
}

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Version 0.1 (2016-07-04)
2+
3+
* Initial version

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Yury Habets
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# UnityAndroidPermissions
2+
Starting with Android Marshmallow (Android 6), Google introduced Runtime permissions system where the user is asked to grant a permission in runtime rather than doing that during installation of the app.
3+
However, Unity for Android is not supporting it out of the box because:
4+
- the corresponding Android API requires an Activity, when Unity can run without it. All non-Activity Unity applications are not supported for that reason
5+
- the plugins may add a dangerous permission and not have the code to handle it correctly, thus causing the whole app to crash
6+
This is the reason why Unity prompts the user for all the permissions on startup. This behavior is the safest and most compatible.
7+
8+
However, Google requires the runtime permission system to be implemented to get featured on Google Play. To let the user implement it (and take the responsibility), the Unity's dialog on startup can be suppressed by adding "unityplayer.SkipPermissionsDialog"="true" metadata tag to application or activity section of the Android Manifest.
9+
10+
This plugin is one of the Android runtime permissions for Unity implementations. It provides the API to check the status of a permission, request a set of permissions and get a callback with the result.
11+
12+
## API
13+
14+
`AndroidPermissionsManager` is the class which provides you the following methods:
15+
- `bool IsPermissionGranted(string permissionName)` to check the status of a permission
16+
- `void RequestPermission(string[] permissionNames, AndroidPermissionCallback callback)` to query for an array of permissions. Pass `AndroidPermissionCallback` object or an object of derived class with your own callback implementation. `void AndroidPermissionCallback.OnPermissionGranted(string permissionName)` is called when a permission is granted, `void OnPermissionDenied(string permissionName)` - when a permission is denied.
17+
18+
## Usage
19+
0. Should work with Unity 5.3+. Please report an issue if it does not for you
20+
1. Add the plugin to your project. You need the manifest, the AAR and the C# script (Assets/Plugins/Android/AndroidManifest.xml, Assets/Plugins/Android/unityandroidpermissions.aar and Assets/AndroidPermissionsManager.cs)
21+
2. Please pay attention to the manifest - you may want to use the one provided here or, if you have your own base manifest, please make sure to add "unityplayer.SkipPermissionsDialog"="true" metadata tag to application or activity section
22+
3. Use the C# API in your scripts to check the permission status and request it if necessary, right before you actually need this permission
23+
4. Enjoy
24+
25+
## How to Build
26+
Use Android Studio to build the AAR from the source in src/ directory.
27+
28+
## See Also
29+
Please refer to Google documentation for more details: https://developer.android.com/training/permissions/requesting.html
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
apply plugin: 'com.android.library'
2+
3+
android {
4+
compileSdkVersion 25
5+
buildToolsVersion "26.0.0"
6+
7+
defaultConfig {
8+
minSdkVersion 16
9+
targetSdkVersion 25
10+
versionCode 1
11+
versionName "1.0"
12+
}
13+
buildTypes {
14+
release {
15+
minifyEnabled false
16+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17+
}
18+
}
19+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in C:\android-sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
18+
19+
# Uncomment this to preserve the line number information for
20+
# debugging stack traces.
21+
#-keepattributes SourceFile,LineNumberTable
22+
23+
# If you keep the line number information, uncomment this to
24+
# hide the original source file name.
25+
#-renamesourcefileattribute SourceFile
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
2+
package="com.example.unityandroidpermissions">
3+
4+
</manifest>

0 commit comments

Comments
 (0)