首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >统一android前置摄像头

统一android前置摄像头
EN

Stack Overflow用户
提问于 2017-10-08 10:16:37
回答 1查看 9K关注 0票数 0

我怎么能改变这个代码来访问前摄像头,目前它显示给我后摄像头。?我试过一些不同的东西,但似乎没有用。

代码语言:javascript
复制
    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 ();
        }


    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-08 10:52:38

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

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

  1. 确定前摄像头的deviceName。
  2. 在创建您的deviceName对象时,使用前面的摄像机使用WebCamTexture。

您可以查询可用相机的设备名称,如下所示:

代码语言:javascript
复制
var webCamDevices = WebCamTexture.devices;
foreach(var camDevice in webCamDevices){ 
    Debug.Log(camDevice.name);
}

此外,isFrontFacing属性WebCamDevice还将有助于查询您使用的相机是否是前置摄像机。因此,确保找到的第一个正面摄像机将用于您的情况的天真方法是:

代码语言:javascript
复制
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()函数中初始化它也是一种很好的做法。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46629961

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档