Requesting Permissions

Always Ask Nicely

Before accessing a camera or microphone, your app must first request permissions to do so from the user. If the user does not grant the necessary permissions, you will not be able to use the corresponding media device.

Checking Permissions

The MediaDeviceQuery class provides the CheckPermissions function to check the current permission status for a given media device type:

// Check camera permissions
PermissionStatus status = MediaDeviceQuery.CheckPermissions<CameraDevice>();
// Log
Debug.Log($"Camera permission status is {status}");

Requesting Permissions

The MediaDeviceQuery class provides the RequestPermissions method to request permissions for a given media device type:

// Request camera permissions
PermissionStatus status = await MediaDeviceQuery.RequestPermissions<CameraDevice>();
// Check if granted
if (status == PermissionStatus.Authorized) {
    // Do stuff
    ...
}

The request is an asynchronous operation. The native OS permissions dialog will be presented to the user, and the returned Task<bool> will complete once the user has responded to the dialog:

On iOS, you must enter a Camera Usage Description in Unity's Player Settings for the camera permission dialog to be shown.

On iOS, you must enter a Microphone Usage Description in Unity's Player Settings for the microphone permission dialog to be shown.

If the user has rejected the permission, calling RequestPermissions will not cause the permission dialog to be displayed again.

Last updated