Device switcher camera in Unity 3d

I am using the WebCamTexture class to open a camera from a device. I want to switch the camera from front to back and vice versa between games. Can anyone please 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 cannot switch the camera.

+3


source to share


3 answers


I got a solution. You just need to provide the device name. The default rear camera is "Camera 0" and "Camera 1" front camera. Stop the camera first, change the installed 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 ();
    }

      

+2


source


I used the following code to switch the front and back camera of an iPhone device using a C # script

WebCamDevice[] devices;

public WebCamTexture mCamera = null;
public GameObject plane; // this is the object where i am going to show the camera

// Use this for initialization
void Start ()
{
    devices = WebCamTexture.devices;

    plane = GameObject.FindWithTag ("Player");

    mCamera = new WebCamTexture (Screen.width,Screen.height);
    mCamera.deviceName = devices[0].name;  // Front camera is at Index 1 and Back Camera is at Index 0
    plane.renderer.material.mainTexture = mCamera;
    mCamera.Play ();

}

      

the above code will show the rear camera when loading the scene, to access the front camera by pressing the front button defined below



    if (GUI.Button (new Rect (100, 1000, 120, 40), "Front"))
    {
        if(devices.Length>1)
         {
            mCamera.Stop();
            mCamera.deviceName = (mCamera.deviceName == devices[0].name) ? devices[1].name : devices[0].name;
            mCamera.Play();
        }

    }

      

This is a button, clicking it will redirect to the front camera and click it again, otherwise it will be redirected to the back camera

0


source


if (devices.Length > 1) {
     webCamTexture.Stop();
    if (frontCamera == true)
    {
        webCamTexture.deviceName = devices[0].name;
        frontCamera = WebCamTexture.devices[0].isFrontFacing;
    }
    else
    {
        webCamTexture.deviceName = devices[1].name;
        frontCamera = WebCamTexture.devices[1].isFrontFacing;
    }
     webCamTexture.Play ();
}

      

We used the Camera Capture Kit for a game / image sharing app to do similar functionality to what you are describing. The other solution presented here is not 100% solid as there could potentially be devices with different order of front and rear devices.

0


source







All Articles