- Home /
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.
Answer by flamy · Feb 06, 2014 at 06:23 AM
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.
this seems like the right idea, but the code is a little off, because cameraIndex is never used. also there's no fallback case. here's what i'm using, tested on iPhone 6 / iOS 10.
webCamTexture = null;
WebCamDevice[] wdcs = WebCamTexture.devices;
for (int n = 0; n < wdcs.Length; ++n) {
if (wdcs[n].isFrontFacing) {
webCamTexture = new WebCamTexture(wdcs[n].name);
}
}
if (webCamTexture == null) {
webCamTexture = new WebCamTexture();
}
i set the width & height later using webCamTexture.requestedWidth
and so on, ins$$anonymous$$d of passing them in the constructor.
Your answer
![](https://koobas.hobune.stream/wayback/20220613132651im_/https://answers.unity.com/themes/thub/images/avi.jpg)