Search code examples
unity-game-enginewebcamunity-webgl

Unity WebGL build Webcam texture not working


I have basic webgl app with Unity3D 2017 where im using webcam texture to display webcam feed on the screen. Here is the code im using to request webcam permission:

yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
    if (Application.HasUserAuthorization(UserAuthorization.WebCam))
    {
        Debug.Log("webcam found");
        startWebCam ();
    }
    else
    {
        Debug.Log("webcam not found");
    }

And here is the code to render webcam feed on a plane that users see.

void startWebCam() {
    WebCamDevice device = WebCamTexture.devices[0];

    cam_texture = new WebCamTexture (device.name);
    camera_renderer.material.mainTexture = cam_texture;
    cam_texture.Play ();
}

It works fine in editor but does not work when i export a webgl build. It does ask for permission to use webcam and then the green light beside my laptop camera turns on but all i can see is black screen. However, the was one time when I think i refreshed twice or something, the webcam feed appeared. I have not been able to reproduce it after that.

Would appreciate any help here. Thanks.


Solution

  • Got the issue resolved. The problem was that autoplay is forbidden on Webgl apps (at least on chrome). So all i had to do is ask user to tap on screen before starting webcam. Fixed.