On Android 6 (API level 23) and above, the Android.Permission API allows you to request permission to use some commonly needed system features, such as the cameraA component which creates an image of a particular viewpoint in your scene. The output is either drawn to the screen or captured as a texture. More info
See in Glossary, microphone, or location, when they are needed rather than when the application starts up. You can request each individual permission when needed and show the user a message that explains the reason why you are asking.
The standard method Android uses for permissions is to show the user a list of the permissions that the app needs when it starts up, providing no explanation as to why or in what context these permissions are used. This can be confusing and some users might deny the permissions even if they are necessary to the operation of the application.
Google’s guideline for requesting permissions recommends that, if the user has denied your permission request once, you should display the reason for the request and give them the opportunity to allow you to present the request again.
You can use the Permission.HasUserAuthorizedPermission function to check whether a permission has been authorized by the user. If it has not, you can request the permission again, explaining why your app needs the permission. Once you have the user’s approval, call Permission.RequestUserPermission to request the permission again. When you call this function, Android opens the system permission dialog to allow the user to approve (or deny) the permission.
If the user still denies the permission request, you should disable the related functionality in your app, or, if the app cannot work without it, inform the user. Note that if the user previously checked the “Do not ask me again” option on the system permission dialog, RequestUserPermission()
does not open the system dialog. The user must go into the application permission settings and manually turn on the permission in this case.
You can add the unityplayer.SkipPermissionsDialog
meta-data to your Android manifest to suppress the permissions dialog normally shown when the app needs to obtain permission from the user to access a protected feature. If you do, you must call Permission.RequestUserPermission at an appropriate time to gain access to each protected feature. See Android Manifest for more information.
For additional information on requesting Android permissions, see App permissions best practices in the Android developer guide.
The following sample code shows how to check whether a permission has been granted for a specific permission and present a dialog in the case where the user has denied the permission:
using UnityEngine;
#if PLATFORM_ANDROID
using UnityEngine.Android;
#endif
public class MicrophoneTest : MonoBehaviour
{
GameObject dialog = null;
void Start ()
{
#if PLATFORM_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
Permission.RequestUserPermission(Permission.Microphone);
dialog = new GameObject();
}
#endif
}
void OnGUI ()
{
#if PLATFORM_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
// The user denied permission to use the microphone.
// Display a message explaining why you need it with Yes/No buttons.
// If the user says yes then present the request again
// Display a dialog here.
dialog.AddComponent<PermissionsRationaleDialog>();
return;
}
else if (dialog != null)
{
Destroy(dialog);
}
#endif
// Now you can do things with the microphone
}
}
using UnityEngine;
#if PLATFORM_ANDROID
using UnityEngine.Android;
#endif
public class PermissionsRationaleDialog : MonoBehaviour
{
const int kDialogWidth = 300;
const int kDialogHeight = 100;
private bool windowOpen = true;
void DoMyWindow(int windowID)
{
GUI.Label(new Rect(10, 20, kDialogWidth - 20, kDialogHeight - 50), "Please let me use the microphone.");
GUI.Button(new Rect(10, kDialogHeight - 30, 100, 20), "No");
if (GUI.Button(new Rect(kDialogWidth - 110, kDialogHeight - 30, 100, 20), "Yes"))
{
#if PLATFORM_ANDROID
Permission.RequestUserPermission(Permission.Microphone);
#endif
windowOpen = false;
}
}
void OnGUI ()
{
if (windowOpen)
{
Rect rect = new Rect((Screen.width / 2) - (kDialogWidth / 2), (Screen.height / 2) - (kDialogHeight / 2), kDialogWidth, kDialogHeight);
GUI.ModalWindow(0, rect, DoMyWindow, "Permissions Request Dialog");
}
}
}
Note: Add MicrophoneTest
to a GameObjectThe fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. A GameObject’s functionality is defined by the Components attached to it. More info
See in Glossary as a component, but not PermissionsRationaleDialog
. MicrophoneTest
automatically creates a GameObject and adds PermissionsRationaleDialog
when it needs to show the dialog. Also note that your app must use the Microphone class for Unity to add the microphone permission.
When you visit any website, it may store or retrieve information on your browser, mostly in the form of cookies. This information might be about you, your preferences or your device and is mostly used to make the site work as you expect it to. The information does not usually directly identify you, but it can give you a more personalized web experience. Because we respect your right to privacy, you can choose not to allow some types of cookies. Click on the different category headings to find out more and change our default settings. However, blocking some types of cookies may impact your experience of the site and the services we are able to offer.
More information
These cookies enable the website to provide enhanced functionality and personalisation. They may be set by us or by third party providers whose services we have added to our pages. If you do not allow these cookies then some or all of these services may not function properly.
These cookies allow us to count visits and traffic sources so we can measure and improve the performance of our site. They help us to know which pages are the most and least popular and see how visitors move around the site. All information these cookies collect is aggregated and therefore anonymous. If you do not allow these cookies we will not know when you have visited our site, and will not be able to monitor its performance.
These cookies may be set through our site by our advertising partners. They may be used by those companies to build a profile of your interests and show you relevant adverts on other sites. They do not store directly personal information, but are based on uniquely identifying your browser and internet device. If you do not allow these cookies, you will experience less targeted advertising. Some 3rd party video providers do not allow video views without targeting cookies. If you are experiencing difficulty viewing a video, you will need to set your cookie preferences for targeting to yes if you wish to view videos from these providers. Unity does not control this.
These cookies are necessary for the website to function and cannot be switched off in our systems. They are usually only set in response to actions made by you which amount to a request for services, such as setting your privacy preferences, logging in or filling in forms. You can set your browser to block or alert you about these cookies, but some parts of the site will not then work. These cookies do not store any personally identifiable information.