Search Unity

Bug WebCamTexture can't select device. (add demo)

Discussion in 'Web' started by gtk2k, Oct 24, 2020.

  1. gtk2k

    gtk2k

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

    Code (CSharp):
    1.  
    2. using System.Linq;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class WebCamTextureTest : MonoBehaviour
    7. {
    8.     public Button btn;
    9.  
    10.     private WebCamDevice[] webCamDevices;
    11.     private int idx = 0;
    12.  
    13.     private void Start()
    14.     {
    15.         webCamDevices = WebCamTexture.devices;
    16.  
    17.         //var deviceNames = string.Join(", ", webCamDevices.Select((x, i) => $"[{i}]:{x.name}"));
    18.         //Debug.Log(deviceNames);
    19.  
    20.         btn.onClick.AddListener(Change);
    21.     }
    22.  
    23.     private void Change()
    24.     {
    25.         idx++;
    26.         if(idx >= webCamDevices.Length)
    27.         {
    28.             idx = 0;
    29.         }
    30.         Debug.Log(webCamDevices[idx].name);
    31.         var tex = new WebCamTexture(webCamDevices[idx].name);
    32.         GetComponent<Renderer>().material.mainTexture = tex;
    33.         tex.Play();
    34.     }
    35. }
    36.  
     
    Last edited: Oct 25, 2020
  2. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    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.
     
    Marks4 likes this.
  3. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    288
    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
    Code (JavaScript):
    1.  
    2. navigator.getMedia (
    3. {
    4.     video: true,
    5.     audio: false
    6. },
    7.  
    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
    Code (JavaScript):
    1.  
    2. navigator.getMedia (
    3. {
    4.     video: {deviceId: "selected deviceId"},
    5.     audio: false
    6. },
    7.  
    The specified camera device cannot be selected unless the deviceId is set as in.
     
  4. jukka_j

    jukka_j

    Unity Technologies

    Joined:
    May 4, 2018
    Posts:
    953
    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.
     
  5. gtk2k

    gtk2k

    Joined:
    Aug 13, 2014
    Posts:
    288
    OK.
    I will wait for the new source to come.
     
  6. gtk2k

    gtk2k

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

    Banglemoose

    Joined:
    Dec 9, 2014
    Posts:
    9
    @gtk2k Can you post your code for the bug fixed build you've shown?