我怎么能改变这个代码来访问前摄像头,目前它显示给我后摄像头。?我试过一些不同的东西,但似乎没有用。
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 ();
}
}发布于 2017-10-08 10:52:38
您当前从默认设备中创建WebcamTexture的代码,因为您使用的构造函数没有传递deviceName的空字符串的任何参数。
如果检查WebCamTexture文档,它将列出可以提供deviceName的构造函数。现在,你要做的就是:
您可以查询可用相机的设备名称,如下所示:
var webCamDevices = WebCamTexture.devices;
foreach(var camDevice in webCamDevices){
Debug.Log(camDevice.name);
}此外,isFrontFacing属性WebCamDevice还将有助于查询您使用的相机是否是前置摄像机。因此,确保找到的第一个正面摄像机将用于您的情况的天真方法是:
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()函数中初始化它也是一种很好的做法。
https://stackoverflow.com/questions/46629961
复制相似问题