vanshika
vanshika

Reputation: 411

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.

Upvotes: 3

Views: 7882

Answers (3)

Chris
Chris

Reputation: 508

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've used Camera Capture Kit for a social image sharing game/app to do a similar functionality to what you are describing, The other solution presented here is not 100% solid, since there might potentially be devices with different order of front and back-facing devices.

Upvotes: 0

Gourav Dixit
Gourav Dixit

Reputation: 191

I have used the following code for switching the front and back camera of the iPhone Device using 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 back Camera when the Scene is loaded ,for accessing the front camera on the click of 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 button, by clicking it will redirect to front camera and again clicking it will redirect to back camera alternatively

Upvotes: 0

vanshika
vanshika

Reputation: 411

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 ();
    }

Upvotes: 3

Related Questions