How to enable the camera, microphone, or WebRTC?

Like a normal browser, 3D WebView blocks web pages from accessing the device's camera and microphone by default. However, applications can use Web.SetCameraAndMicrophoneEnabled() to enable camera and microphone access. On Windows and WebGL, no additional configuration is needed. However, on macOS, Android, iOS, and UWP, additional project configuration is needed in order to successfully grant camera and microphone access:

For additional WebRTC topics, please see these sections:

macOS camera and mic access

For macOS, the following two steps are required in addition to calling Web.SetCameraAndMicrophoneEnabled():

  1. To use the camera and microphone while running in the macOS Unity editor, it's necessary to go to the macOS System Settings app, click "Privacy & Security", and then use the "Camera" and "Microphone" menu options to enable camera and microphone access for Unity Hub.

macOS security settings macOS camera settings macOS mic settings

  1. To use the camera and microphone in a macOS player build, it's necessary to go to Unity macOS Player Settings -> Other Settings -> Configuration and fill in text for the "Camera Usage Description" and "Microphone Usage Description" fields:

macOS player settings

Android camera and mic access

For Android, the following two steps are required in addition to calling Web.SetCameraAndMicrophoneEnabled():

  1. The application's AndroidManifest.xml file must include the following permissions, as demonstrated in this example AndroidManifest.xml file:

  2. The application must also request camera and microphone permissions at runtime. Alternatively, during development, you can manually grant camera and microphone permissions to the app via the Android Settings app.

iOS camera and mic access

For iOS, enabling the camera and microphone is only supported in iOS 14.3 or newer and is only supported in Native 2D Mode. In addition to calling Web.SetCameraAndMicrophoneEnabled(), you must also enter text for the "Camera Usage Description" and "Microphone Usage Description" in iOS Player Settings -> Other Settings:

iOS camera and mic player settings

  • When the application calls Web.SetCameraAndMicrophoneEnabled() for the very first time, the operating system displays permission prompts containing the text that you entered for the camera and microphone usage descriptions in Player Settings. If the user grants permission, then this step occurs only once.

  • Also, every time that the app runs, the operating system displays a separate permission prompt containing the web page's domain when it requests camera or mic access. Unlike the first permission prompt, this one is reshown every time the app runs. There is no way around this because it's how camera and microphone access in WKWebView is designed, and it's also how the mobile Safari browser behaves.

  • Not all websites that use the camera and microphone correctly support mobile Safari. Here's an example of one that does, which can be used for testing.

UWP camera and mic access

For UWP, in addition to calling Web.SetCameraAndMicrophoneEnabled(), you must also enable the "WebCam" and "Microphone" capabilities in UWP Player Settings -> Publishing Settings. Also, if you have an existing UWP build directory, you will need to delete it after adding these capabilities so that Unity regenerates the app's manifest file with the new capabilities.

UWP camera and mic player settings

How to select which camera, microphone, or speaker is used?

For WebRTC, the web page itself can choose which camera, microphone, and speaker is used by calling the enumerateDevices() JavaScript API to view the available devices. For more info, please see the following resources: