Comment on page
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.
The
MediaDeviceQuery
class provides the CheckPermissions
function to check the current permission status for a given media device type:Camera
Microphone
// Check camera permissions
PermissionStatus status = MediaDeviceQuery.CheckPermissions<CameraDevice>();
// Log
Debug.Log($"Camera permission status is {status}");
// Check microphone permissions
PermissionStatus status = MediaDeviceQuery.CheckPermissions<AudioDevice>();
// Log
Debug.Log($"Microphone permission status is {status}");
The
MediaDeviceQuery
class provides the RequestPermissions
method to request permissions for a given media device type:Camera
Microphone
// Request camera permissions
PermissionStatus status = await MediaDeviceQuery.RequestPermissions<CameraDevice>();
// Check if granted
if (status == PermissionStatus.Authorized) {
// Do stuff
...
}
// Request microphone permissions
PermissionStatus status = await MediaDeviceQuery.RequestPermissions<AudioDevice>();
// 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:
The iOS permissions 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 modified 1yr ago