Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch device camera in Unity 3d

I am using WebCamTexture class to open camera from device. I want to switch camera from front to back and vice versa in between the game play. Can anybody help?

    WebCamDevice[] devices = WebCamTexture.devices;
    WebCamTexture   webCamTexture = new WebCamTexture(devices[1].name);
    renderer.material.mainTexture = webCamTexture;
    webCamTexture.filterMode = FilterMode.Trilinear;
    webCamTexture.Play();

This opens my front camera. But I can't switch the camera.

like image 382
vanshika Avatar asked Jul 03 '26 23:07

vanshika


1 Answers

I got the solution. You just need to set the name of the device. By default back camera is "Camera 0" and "Camera 1" front camera. First Stop your camera , change the set device.

if (devices.Length > 1) {
         webCamTexture.Stop();
        if (frontCamera == true)
        {
            webCamTexture.deviceName = devices[0].name;
            frontCamera = false;
        }
        else
        {
            webCamTexture.deviceName = devices[1].name;
            frontCamera = true;
        }
         webCamTexture.Play ();
    }
like image 69
vanshika Avatar answered Jul 05 '26 14:07

vanshika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!