0

我如何更改此代码以访问前置摄像头,目前它显示给我后置摄像头。?我尝试了一些不同的东西,但似乎没有用。

    if (isLocalPlayer)
    {
        if (this.gameObject.name == "Player 1") {
            WebCamTexture webcamTexture = new WebCamTexture ();
            Player1Image.GetComponent<RawImage> ().texture = webcamTexture;
            Player1Image.GetComponent<RawImage> ().texture = webcamTexture;
            Player1Image.GetComponent<RawImage> ().material.mainTexture = webcamTexture;
            webcamTexture.Play ();
        } else if (this.gameObject.name == "Player 2") {
            WebCamTexture webcamTexture = new WebCamTexture ();
            Player2Image.GetComponent<RawImage> ().texture = webcamTexture;
            Player2Image.GetComponent<RawImage> ().texture = webcamTexture;
            Player2Image.GetComponent<RawImage> ().material.mainTexture = webcamTexture;
            webcamTexture.Play ();
        } else if (this.gameObject.name == "Player 3") {
            WebCamTexture webcamTexture = new WebCamTexture ();
            Player3Image.GetComponent<RawImage> ().texture = webcamTexture;
            Player3Image.GetComponent<RawImage> ().texture = webcamTexture;
            Player3Image.GetComponent<RawImage> ().material.mainTexture = webcamTexture;
            webcamTexture.Play ();
        } else if (this.gameObject.name == "Player 4") {
            WebCamTexture webcamTexture = new WebCamTexture ();
            Player4Image.GetComponent<RawImage> ().texture = webcamTexture;
            Player4Image.GetComponent<RawImage> ().texture = webcamTexture;
            Player4Image.GetComponent<RawImage> ().material.mainTexture = webcamTexture;
            webcamTexture.Play ();
        } else {
            Debug.Log ("All slots full!");
            GameObject.Destroy (this);
            Network.Disconnect ();
        }


    }
4

1 回答 1

3

您当前拥有的代码从默认设备创建一个 WebcamTexture,因为您使用的构造函数没有任何参数,它为deviceName传递一个空字符串。

如果您查看WebCamTexture 文档,它会列出您可以提供deviceName的构造函数。现在,您所要做的就是:

  1. 确定前置摄像头的deviceName
  2. 在创建 WebCamTexture 对象时,将deviceName用于前置摄像头。

您可以通过以下方式查询可用摄像机的设备名称:

var webCamDevices = WebCamTexture.devices;
foreach(var camDevice in webCamDevices){ 
    Debug.Log(camDevice.name);
}

此外,WebCamDevice 的isFrontFacing属性也有助于查询您使用的摄像头是否为前置摄像头。因此,确保找到的第一个前置摄像头将用于您的案例的一种天真的方法是:

if (isLocalPlayer)
{
    string frontCamName = null;
    var webCamDevices = WebCamTexture.devices;
    foreach(var camDevice in webCamDevices){ 
        if(camDevice.isFrontFacing){
            frontCamName = camDevice.name;
            break;
        }
    }
    if (this.gameObject.name == "Player 1") {
        WebCamTexture webcamTexture = new WebCamTexture (frontCamName);
        Player1Image.GetComponent<RawImage> ().texture = webcamTexture;
        Player1Image.GetComponent<RawImage> ().texture = webcamTexture;
        Player1Image.GetComponent<RawImage> ().material.mainTexture = webcamTexture;
        webcamTexture.Play ();
    } else if (this.gameObject.name == "Player 2") {
        WebCamTexture webcamTexture = new WebCamTexture (frontCamName);
        Player2Image.GetComponent<RawImage> ().texture = webcamTexture;
        Player2Image.GetComponent<RawImage> ().texture = webcamTexture;
        Player2Image.GetComponent<RawImage> ().material.mainTexture = webcamTexture;
        webcamTexture.Play ();
    } else if (this.gameObject.name == "Player 3") {
        WebCamTexture webcamTexture = new WebCamTexture (frontCamName);
        Player3Image.GetComponent<RawImage> ().texture = webcamTexture;
        Player3Image.GetComponent<RawImage> ().texture = webcamTexture;
        Player3Image.GetComponent<RawImage> ().material.mainTexture = webcamTexture;
        webcamTexture.Play ();
    } else if (this.gameObject.name == "Player 4") {
        WebCamTexture webcamTexture = new WebCamTexture (frontCamName);
        Player4Image.GetComponent<RawImage> ().texture = webcamTexture;
        Player4Image.GetComponent<RawImage> ().texture = webcamTexture;
        Player4Image.GetComponent<RawImage> ().material.mainTexture = webcamTexture;
        webcamTexture.Play ();
    } else {
        Debug.Log ("All slots full!");
        GameObject.Destroy (this);
        Network.Disconnect ();
    }
}

请注意,通过删除break语句,您将使用最后列出的前置摄像头。此外,最好将frontCamName 设为私有属性并在 Start() 函数中对其进行初始化。

于 2017-10-08T10:52:38.970 回答