在Unity 3D中切换设备摄像头

3

我正在使用WebCamTexture类从设备打开相机。我希望在游戏过程中能够在前置和后置摄像头之间切换。有人能帮忙吗?

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

这打开了我的前置摄像头。但我无法切换相机。

3个回答

3

我有解决方案。您只需要设置设备的名称即可。默认情况下,后置摄像头为“Camera 0”,前置摄像头为“Camera 1”。先停止相机,更改已设置的设备。

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

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

我们曾经在一个社交图片分享游戏/应用中使用了Camera Capture Kit来实现类似于您所描述的功能。这里提出的另一种解决方案并不完全可靠,因为可能存在前置和后置设备顺序不同的设备。

0
我使用了以下代码来使用C#脚本切换iPhone设备的前置和后置摄像头。
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 ();

}

上面的代码将在场景加载时显示后置摄像头,为了在下面定义的前置按钮点击时访问前置摄像头。
    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();
        }

    }

这是一个按钮,点击它将会重定向到前置摄像头,再次点击将会交替地重定向到后置摄像头。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接