Search Unity

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

Discussion in 'Web' started by HasbullaPotok, Aug 21, 2022.

  1. HasbullaPotok

    HasbullaPotok

    Joined:
    Jun 7, 2022
    Posts:
    2
    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 doesn`t 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 device`s 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 :(

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4.  
    5. namespace MultiWebcam
    6. {
    7.     public sealed class CameraController : MonoBehaviour
    8.     {
    9.         [SerializeField] private RawImage _display;
    10.  
    11.         private WebCamDevice[] devices;
    12.         private WebCamTexture _texture;
    13.         private int _currentCameraIndex = 0;
    14.  
    15.         private void Awake()
    16.         {
    17.             StartCoroutine(Start());
    18.         }
    19.  
    20.         private IEnumerator Start()
    21.         {
    22.             yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
    23.             if (Application.HasUserAuthorization(UserAuthorization.WebCam))
    24.             {
    25.                 Debug.Log("webcam found");
    26.                 devices = WebCamTexture.devices;
    27.                 for (int cameraIndex = 0; cameraIndex < devices.Length; ++cameraIndex)
    28.                 {
    29.                     Debug.Log("devices[cameraIndex].name: ");
    30.                     Debug.Log(devices[cameraIndex].name);
    31.                     Debug.Log("devices[cameraIndex].isFrontFacing");
    32.                     Debug.Log(devices[cameraIndex].isFrontFacing);
    33.                 }
    34.             }
    35.             else
    36.             {
    37.                 Debug.Log("no webcams found");
    38.             }
    39.         }
    40.  
    41.         public void SwapCamera()
    42.         {
    43.             if (WebCamTexture.devices.Length > 0)
    44.             {
    45.                 _currentCameraIndex++;
    46.                 _currentCameraIndex %= WebCamTexture.devices.Length;
    47.  
    48.                 if (_texture != null)
    49.                 {
    50.                     StopCamera();
    51.                     StartCamera();
    52.                 }
    53.             }
    54.         }
    55.  
    56.         public void StartCamera()
    57.         {
    58.             Debug.LogError($"USER PERMISSION {Application.HasUserAuthorization(UserAuthorization.WebCam)}");
    59.             Debug.LogError($"DEVICES AMOUNT {WebCamTexture.devices.Length}");
    60.  
    61.             if (WebCamTexture.devices.Length > 0)
    62.             {
    63.                 WebCamDevice device = WebCamTexture.devices[_currentCameraIndex];
    64.                 _texture = new WebCamTexture(device.name);
    65.                 _display.texture = _texture;
    66.  
    67.                 _texture.Play();
    68.                 Debug.LogError($"START PLAYING!");
    69.             }
    70.         }
    71.  
    72.         public void StopCamera()
    73.         {
    74.             if (_texture != null)
    75.             {
    76.                 _texture.Stop();
    77.                 _display.texture = null;
    78.                 _texture = null;
    79.             }
    80.         }
    81.     }
    82. }
    83.  
    84.  

    upload_2022-8-21_15-13-6.png
     
    Last edited: Aug 21, 2022
  2. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    288
    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.
     

    Attached Files:

    HasbullaPotok likes this.
  3. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    5,063
  4. HasbullaPotok

    HasbullaPotok

    Joined:
    Jun 7, 2022
    Posts:
    2
    Thank you guys, the problem was that my app was on http, not https :D
     
  5. unityruba

    unityruba

    Unity Technologies

    Joined:
    Nov 6, 2020
    Posts:
    273
    @HasbullaPotok ahhh, yes! I'll make sure to add that to our docs!
     
    DevDunk likes this.
  6. ShivNandi

    ShivNandi

    Joined:
    Mar 24, 2017
    Posts:
    2
    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.
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,205
    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/
     
    Last edited: Apr 23, 2023
  8. ngothien

    ngothien

    Joined:
    Feb 4, 2020
    Posts:
    1
    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?
     
    yuriachermann likes this.