Skip to main content
Version: 14 Oct 2024

Android Permissions in Unity

This page will cover configuring Permissions within Unity for your application. Permissions are an important aspect of preparing your application for deployment to the device.

The MLPermissions API in the Unity SDK is provided as a layer between the ML developer and Unity's built-in Android Permissions API that enables integration with App Simulator, and implements MLResult return codes (consistent with all other ML API) based on permission state.

Before continuing, you should have a very basic grasp of Android's permissions theory, specifically:

  1. Permissions Overview
  2. Declaring permissions
  3. Requesting User Permissions
  4. Permissions Callbacks

Android Permissions

Android permissions are not displayed in the Manifest Settings panel, only Magic Leap specific ones. Android permissions can be added manually when required. Unity provides some settings for various Android specific permissions within the Project Settings > Player Settings > Android Platform (Android Icon) > Other Settings such as Internet Access and Write Permission as shown:

Android Permissions

These will not appear in the AndroidManifest.xml but will be automatically added when the APK is generated based on the settings selected. You can also manually add them to the AndroidManifest.xml as needed. Refer to the Android documentation on required permissions for each API. You can see the full list of permissions on Android's Permission documentation.

Checking Permissions

Use Permission.HasUserAuthorizedPermission to check if the user has already granted permission for the data or feature the application requires. For a code example that shows how to use this API, refer to this code block from Unity:

using UnityEngine;
using UnityEngine.Android;

public class CheckPermissionScript : MonoBehaviour
{
void Start()
{
if (Permission.HasUserAuthorizedPermission(Permission.Microphone))
Debug.Log("Microphone permission has been granted.");
}
}

Check whether to display the rationale for permission request

Use Permission.ShouldShowRequestPermissionRationale to check whether you need to display the rationale for a specific permission request.

If the rationale is necessary, display a message with reason why your application requires access to specific device features. After you display the message, send a request for permission. If the rationale isn’t necessary, directly proceed to send a request for permission.

For a code example that shows how to use this API, refer to this code block from Unity:

using UnityEngine;
using UnityEngine.Android;

public class RequestPermissionScript : MonoBehaviour
{
internal void PermissionCallbacks_PermissionDeniedAndDontAskAgain(string permissionName)
{
Debug.Log($"{permissionName} PermissionDeniedAndDontAskAgain");
}

internal void PermissionCallbacks_PermissionGranted(string permissionName)
{
Debug.Log($"{permissionName} PermissionCallbacks_PermissionGranted");
}

internal void PermissionCallbacks_PermissionDenied(string permissionName)
{
Debug.Log($"{permissionName} PermissionCallbacks_PermissionDenied");
}

void Start()
{
if (Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
// The user authorized use of the microphone.
}
else
{
bool useCallbacks = false;
if (!useCallbacks)
{// We do not have permission to use the microphone.
// Check whether you need to display the rationale for requesting permission
if (Permission.ShouldShowRequestPermissionRationale(Permission.Microphone))
{
// Show a message or inform the user in other ways why your application needs the microphone permission.
}
// Ask for permission or proceed without the functionality enabled.
Permission.RequestUserPermission(Permission.Microphone);
}
else
{
var callbacks = new PermissionCallbacks();
callbacks.PermissionDenied += PermissionCallbacks_PermissionDenied;
callbacks.PermissionGranted += PermissionCallbacks_PermissionGranted;
callbacks.PermissionDeniedAndDontAskAgain += PermissionCallbacks_PermissionDeniedAndDontAskAgain;
Permission.RequestUserPermission(Permission.Microphone, callbacks);
}
}
}
}

Request Permission

Use Permission.RequestUserPermission to request permission to use the data or feature. When you call this method, Android opens the system permission dialog that the user can use to grant or deny the permission. For a code example that shows how to use this API, refer to the following code block from Unity:

using UnityEngine;
using UnityEngine.Android;

public class RequestPermissionScript : MonoBehaviour
{
internal void PermissionCallbacks_PermissionDeniedAndDontAskAgain(string permissionName)
{
Debug.Log($"{permissionName} PermissionDeniedAndDontAskAgain");
}

internal void PermissionCallbacks_PermissionGranted(string permissionName)
{
Debug.Log($"{permissionName} PermissionCallbacks_PermissionGranted");
}

internal void PermissionCallbacks_PermissionDenied(string permissionName)
{
Debug.Log($"{permissionName} PermissionCallbacks_PermissionDenied");
}

void Start()
{
if (Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
// The user authorized use of the microphone.
}
else
{
bool useCallbacks = false;
if (!useCallbacks)
{
// We do not have permission to use the microphone.
// Ask for permission or proceed without the functionality enabled.
Permission.RequestUserPermission(Permission.Microphone);
}
else
{
var callbacks = new PermissionCallbacks();
callbacks.PermissionDenied += PermissionCallbacks_PermissionDenied;
callbacks.PermissionGranted += PermissionCallbacks_PermissionGranted;
callbacks.PermissionDeniedAndDontAskAgain += PermissionCallbacks_PermissionDeniedAndDontAskAgain;
Permission.RequestUserPermission(Permission.Microphone, callbacks);
}
}
}
}

Use Permission.RequestUserPermissions to request permissions to access multiple resources on the user’s device at once. This method uses an array of strings with each string representing a specific permission to access a particular resource such as the device’s camera, microphone, or location.

These methods can accept a PermissionCallbacks object that you can use to specify code to run after the user grants or denies the permission. You can use this to start using a device feature as soon as the user grants the permission request. For example, you can start recording from the microphone.