How to get an access to a device camera Unity WebGL?

Hello everybody! Im trying to build up a simple WebGL app with Unity and get the access to devices camera in browser. I found out some solutions, but it doesnt work for me, can anybody help to solve it please? I was also googling this problem, but there was no any solutions... I Tried these two methods below, it works good if I build an apk or windows build but for WebGL it doesnt. When I build it and run it shows 0 devices, has an access rights , but 0 devices. Tried WebGL build on android, ios, macOs, but it always shows 0 devices and I never get a UserAuthorization request to provide an access to my devices cameras. My full script is below. I tried it on Unity version: 2022.2.0b4, 2022.1.1f1, 2021.3.6f1. Browsers: Google, Safari

StartCamera, StopCamera, SwapCamera methods are linked to Buttons in inspector

PLEASE ANYBODY HELP :frowning:

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

namespace MultiWebcam
{
    public sealed class CameraController : MonoBehaviour
    {
        [SerializeField] private RawImage _display;

        private WebCamDevice[] devices;
        private WebCamTexture _texture;
        private int _currentCameraIndex = 0;

        private void Awake()
        {
            StartCoroutine(Start());
        }

        private IEnumerator Start()
        {
            yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
            if (Application.HasUserAuthorization(UserAuthorization.WebCam))
            {
                Debug.Log("webcam found");
                devices = WebCamTexture.devices;
                for (int cameraIndex = 0; cameraIndex < devices.Length; ++cameraIndex)
                {
                    Debug.Log("devices[cameraIndex].name: ");
                    Debug.Log(devices[cameraIndex].name);
                    Debug.Log("devices[cameraIndex].isFrontFacing");
                    Debug.Log(devices[cameraIndex].isFrontFacing);
                }
            }
            else
            {
                Debug.Log("no webcams found");
            }
        }

        public void SwapCamera()
        {
            if (WebCamTexture.devices.Length > 0)
            {
                _currentCameraIndex++;
                _currentCameraIndex %= WebCamTexture.devices.Length;

                if (_texture != null)
                {
                    StopCamera();
                    StartCamera();
                }
            }
        }

        public void StartCamera()
        {
            Debug.LogError($"USER PERMISSION {Application.HasUserAuthorization(UserAuthorization.WebCam)}");
            Debug.LogError($"DEVICES AMOUNT {WebCamTexture.devices.Length}");

            if (WebCamTexture.devices.Length > 0)
            {
                WebCamDevice device = WebCamTexture.devices[_currentCameraIndex];
                _texture = new WebCamTexture(device.name);
                _display.texture = _texture;

                _texture.Play();
                Debug.LogError($"START PLAYING!");
            }
        }

        public void StopCamera()
        {
            if (_texture != null)
            {
                _texture.Stop();
                _display.texture = null;
                _texture = null;
            }
        }
    }
}

I tried it on 2021.3.6f1 and was able to access the camera.
As a trial, click the icon on the left of the address bar, click “Reset Permission”, and then reload.


8379993--1104951--ResetPermission.png

1 Like

Official docs: Unity - Manual: WebGL browser access to device features

2 Likes

Thank you guys, the problem was that my app was on http, not https :smile:

@HasbullaPotok ahhh, yes! I’ll make sure to add that to our docs!

1 Like

I don’t get it I tried a lot of things although I got permission to access my Mobile Camera but cannot get that camera to work.

If you haven’t already you should verify that it works outside of Unity. Just go to the following website and under “Testing Area” select the appropriate camera and click “Test my cam”. I’ve verified that it works with an iPad 9th 2021 (latest iOS) and Pixel 3a (Android 12.1).

https://webcamtests.com/

I have the same problem, that I want to access my camera on iPhone via Webgl, but I don’t how to do that! Does someone help me?

1 Like