使用Vuforia和Fungus在Unity中更换前后摄像头

时间:2018-03-03 14:22:36

标签: unity3d vuforia

我是Unity和Vuforia的新手。

我一直试图通过使用UI按钮找出如何在Vuforia中的两种相机模式之间进行更改。

所以我想要实现的是在应用程序运行时能够在前后摄像头之间进行切换。

我已将以下脚本链接到UI按钮及其内置的OnClickEvent,但由于未编写DebugLog消息,因此它似乎无法正常工作。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;

public class CameraSwap : MonoBehaviour {

    bool cameramode = false;
    public void OnCameraChangeMode()
    {
        Vuforia.CameraDevice.CameraDirection currentDir = Vuforia.CameraDevice.Instance.GetCameraDirection();
        if (!cameramode)
        {
            RestartCamera(Vuforia.CameraDevice.CameraDirection.CAMERA_FRONT);
            Debug.Log("Using FRONT Camera");
        }
        else
        {
            RestartCamera(Vuforia.CameraDevice.CameraDirection.CAMERA_BACK);
            Debug.Log("Using BAC Camera");
        }
    }

    private void RestartCamera(Vuforia.CameraDevice.CameraDirection newDir)
    {
        Vuforia.CameraDevice.Instance.Stop();
        Vuforia.CameraDevice.Instance.Deinit();
        Vuforia.CameraDevice.Instance.Init(newDir);
        Vuforia.CameraDevice.Instance.Start();
    }
}

1 个答案:

答案 0 :(得分:0)

这里的建议是正确的-只需从可用功能中选择即可。 enter image description here

对于那些难以使用Unity / Vuforia制作相机翻转脚本的人,这是整个脚本的有效示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Vuforia;


public class CameraController : MonoBehaviour 
{
    private bool lightOn = false;
    private bool frontCamera = false;

    public void CameraChange()
    {
        if (!frontCamera)
        {
            RestartCamera(CameraDevice.CameraDirection.CAMERA_FRONT);
            frontCamera = true;
            Debug.Log("Using Front Camera");
        }
        else if (frontCamera)
        {
            RestartCamera(CameraDevice.CameraDirection.CAMERA_BACK);
            frontCamera = false;
            Debug.Log("Using Back Camera");
        }
        else
        {
            Debug.Log("No camera status available.");
        }
    } 

    private void RestartCamera(CameraDevice.CameraDirection newDir)
    {
        CameraDevice.Instance.Stop();
        CameraDevice.Instance.Deinit();
        CameraDevice.Instance.Init(newDir);
        CameraDevice.Instance.Start();

        // These are to fight the bug the will otherwise flip the selfie cam upside down on iOS
        // Periodically check to see if still needed
        VuforiaUnity.OnPause();
        VuforiaUnity.OnResume();
    }
}