how could i change this code to access front cam, currently it shows me back cam.? I have tried some different things, but doesn't seem to be working.
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 ();
}
}
The code that you have currently creates a WebcamTexture from the default device, as you are using the constructor without any parameters which passes a null string for the deviceName.
If you check the WebCamTexture documentation, it lists a constructor that you can provide a deviceName. Now, all you have to do is to:
You can query the device names for the available cameras as follows:
var webCamDevices = WebCamTexture.devices;
foreach(var camDevice in webCamDevices){
Debug.Log(camDevice.name);
}
Also, the isFrontFacing property of WebCamDevice would also help to query whether the camera you are using is the front camera. Therefore, a naive way of making sure that the first front-facing camera that is found will be used for your case would be:
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 ();
}
}
Note that, by removing the break statement, you will use the front-facing camera that is listed last. Also, it would be a good practice to make frontCamName a private property and initialize it in the Start() function.