Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Webcam in WebGL

Discussion in 'Web' started by dax4dan, Nov 3, 2020.

  1. dax4dan

    dax4dan

    Joined:
    Oct 12, 2020
    Posts:
    1
    Hello!

    I'm trying to make a quick game that uses webcam for very basic motion control.
    (Using 2019.4, no external plugins)
    Everything seems to work fine when it is in Unity Editor mode, but once I build it does not work.

    To be more explicit:
    When I load the game I have the following code in
    Start
    :
    Code (CSharp):
    1.  
    2.         yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
    3.  
    4.         if (Application.HasUserAuthorization(UserAuthorization.WebCam))
    5.         {
    6.             Debug.Log("Number of webcams:" + WebCamTexture.devices.Length);
    7.             WebCamDevice device = WebCamTexture.devices[0];
    8.             Debug.Log("Device Name :" + device.name);
    9.             Debug.Log("webcam found");
    10.         }
    11.         else
    12.         {
    13.             Debug.Log("Webcam not found");
    14.         }
    Then I access the texture using

    WebCamDevice activeDevice = WebCamTexture.devices[0];

    in the appropriate class.


    Once I build and upload (e.g. to simmer.io) I then get the following print outs:

    Number of webcams:1
    Device Name :device #0
    webcam found


    But once the game starts, it does not recognize the webcam.
    I am definitely missing a step here as chrome/safari is never really asking me if I want to allow camera or not.

    Could someone help me understand what I am missing?
     
  2. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    279
    It seems that the iframe tag requires sandbox = "allow-same-origin". Previously it was possible to access the camera without this attribute, but now it seems that it has become more stringent to protect privacy and it is necessary to set such an attribute.
    Therefore, it is necessary to add the sandbox attribute in addition to the allow attribute to services such as swimmer.io.

    iframe can camera access sample
    https://jsfiddle.net/jib1/hut751ep/
     
  3. Marks4

    Marks4

    Joined:
    Feb 25, 2018
    Posts:
    497
    Accessing the webcam on webgl is currently not very well supported. Which will very soon change on the next Unity versions.

    I made this asset to deal with the current situation, it will still have its own advantages once webcam is properly supported, if you want to check it out.
     
    Blarp likes this.