WebCamTexture can't select device. (add demo)

With multiple webcams connected
Even if I write this code, Debug.Log() will display the correct device name, but the texture displayed will be the texture of devices [0].

I want to post a pull request for this bugfix.

Without bug fix demo
Bug fixed demo

using System.Linq;
using UnityEngine;
using UnityEngine.UI;

public class WebCamTextureTest : MonoBehaviour
{
    public Button btn;

    private WebCamDevice[] webCamDevices;
    private int idx = 0;

    private void Start()
    {
        webCamDevices = WebCamTexture.devices;

        //var deviceNames = string.Join(", ", webCamDevices.Select((x, i) => $"[{i}]:{x.name}"));
        //Debug.Log(deviceNames);

        btn.onClick.AddListener(Change);
    }

    private void Change()
    {
        idx++;
        if(idx >= webCamDevices.Length)
        {
            idx = 0;
        }
        Debug.Log(webCamDevices[idx].name);
        var tex = new WebCamTexture(webCamDevices[idx].name);
        GetComponent<Renderer>().material.mainTexture = tex;
        tex.Play();
    }
}

This is something that has been recently fixed in our trunk codebase (the WebGL WebCam backend was rewritten). Look out for a new updated point release on 2019.4 or newer, and check out 2021.1 alpha release channel, which is where it will land first.

1 Like

Thanx reply.

I installed 2021.1.0a2 and looked at the source of WebCam.js, but the bug was still not fixed.
Around line 97 of WebCam.js

navigator.getMedia (
{
    video: true,
    audio: false
},

If this is the case, the default camera will be selected.
Therefore, even if you switch devices on the Unity side, it will remain the default device forever.
at least

navigator.getMedia (
{
    video: {deviceId: "selected deviceId"},
    audio: false
},

The specified camera device cannot be selected unless the deviceId is set as in.

If you are seeing a line “navigator.getMedia”, that is still part of the old code, so looks like we have not yet had a release go out with the new implementation.

OK.
I will wait for the new source to come.

@jukka_j
2021.1.0a5 has not been implemented yet.
Could you please tell me which version of the alpha channel it will be implemented in?
Thanx.

@gtk2k Can you post your code for the bug fixed build you’ve shown?