I've installed Android Support Library but in the developer.android site says that for implementing it on my project I need to edit my build.gradle file that I don't have because it's an Unity project.
I've created a build.gradle file copying the content of this website: http://gradleplease.appspot.com/ and I put that file on the root of my Unity project but when I try to use the library it does not work
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.READ_CONTACTS)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.READ_CONTACTS)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
In addition to Jason Knight's post (which I used for my own Unity plugin for handling runtime permissions):
I used Android studio for creating a plugin. I followed the directions on the following site and worked perfectly: http://www.thegamecontriver.com/2015/04/android-plugin-unity-android-studio.html
I also added another method using the shouldShowRequestPermissionRationale() function so I'm able to hide certain UI elements if the user denied the permission and checked the "Don't ask again" checkbox.
The other answers (especially Jason Knight's) have been super helpful to me, but I had to adjust the code to get it to work, so I'm sharing those changes here.
As noted in the comments, that code has this error in Android Studio:
final Fragment request = new Fragment();
part saying "Fragments should be static such that they can be re-instantiated by the system, and anonymous classes are not static"Now I'm not a Java expert so maybe I did things wrong, but I tried to adjust things as explained here: Fragments should be static such that they can be re-instantiated by the system, and anonymous classes are not static
Mostly, I split the Fragment into a new class, so that it's not an anonymous class. So now there are two Java files:
That's the main plugin class, and here's the fragment:
And for completeness here's the C# within Unity:
You need Java code to ask for permission, and you need an interface into said Java code from Unity's C# runtime. You need to create a Unity Plugin to do this.
Below is the plugin that I've created to grant the WRITE_EXTERNAL_STORAGE permission at runtime.
You need a project structure like this:
NoodlePermissionGranter.cs:
NoodlePermissionGranter.java:
BuildNoodlePermissionGranter.sh
You need project.properties and a dummy AndroidManifest.xml in order to have Unity package a jar outside of Plugins/Android/libs
project.properties
AndroidManifest.xml
It'd be nice if the PermissionRequestCallback supplied the requested permission enum as a parameter, but UnityPlayer.UnitySendMessage only supports a single string argument and I decided not to implemented the string serialization (using JSON to do this would be a good choice).
Well if you can use Android Studio and write java codes then...
And below that
Then you can basically call the RequestPermissions(); in OnCreate Method
And the most important thing here you need support-compat-25.1.0.aar file (or the last version of this file) from your
Put the support-compat-25.1.0.aar file in to Assets/Plugins/Android with your other plugin files
Thats it. in addition to that you can use hawkwood's manifest example for disable unity's extra permissions since you created your own.
Another addition to Jason's excellent code for Unity 5.3.3 and up (I'm using 5.4), I added this to the manifest to block Unity from automatically asking at launch:
I've used Jason knight's answer to create this plugin that does the job, the whole code is available on the github repo.
There is also unity package file for easy integration.