Cant access Front (Device) Camera

I am accessing the device camera with the following lines of code:

for (int cameraIndex = 0; cameraIndex < WebCamTexture.devices.Length; cameraIndex++) {
	if (WebCamTexture.devices[cameraIndex].isFrontFacing == true) {
		webCameraTexture	=	new WebCamTexture(cameraIndex, Screen.width, Screen.height);
	}
}
cameraFrame.renderer.material.mainTexture = webCameraTexture;
webCameraTexture.Play();

But I couldn’t access the front camera in both iOS and Android by checking for isFrontFacing flag. Logging the WebCamTexture.devices array returns 2 devices one with isFrontFacing = true and other with isFrontFacing = false. But the new WebCamTexture seems to be linked to the back camera in both the cases.

I am using Unity 4.3.0f4.

1 Answer

1

you are actually creating a webcam texture when the device of certain index is front facing, which is nothing but same as creating a webcamtexture new WebCamTexture(cameraIndex, Screen.width, Screen.height); this line.

Instead you have to create the webcamtexture and set the target device to listen to inside tht for loop. the below is the sample that might work . I havnt tested it in device though. But i am positive it should work

 WebCamDevice[] devices = WebCamTexture.devices;
 
 foreach(WebcamDevice cam in devices)
 {
	if(cam.isFrontFacing )
	{	
		webCameraTexture  =    new WebCamTexture(cameraIndex, Screen.width, Screen.height);
		webCameraTexture.deviceName  = cam.name;
		webCameraTexture.Play();
	}
 }

Unless the API has changed since this question was asked, I believe the issue was actually that new WebCamTexture(cameraIndex, Screen.width, Screen.height); resolves to the constructor WebCamTexture(int requestedWidth, int requestedHeight, int requestedFPS). So, the cameraIndex value was actually being passed to requestedWidth, and so on. If a device name was passed as the first parameter, the rest of the code posted in the original question would have worked fine.