繁体   English   中英

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的空字符串。

如果查看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()函数中对其进行初始化将是一个好习惯。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM