Unity android 前置摄像头

Unity android front camera

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

    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 ();
        }


    }

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

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

  1. 确定前置摄像头的设备名称
  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() 函数中对其进行初始化。