Reputation: 33
I am new to both Unity and Vuforia.
I have been trying to figure out how to change between the two camera modes in Vuforia by using a UI button.
So what I am trying to achieve is being able to change between front and back camera while the app is running.
I have linked the following script to the UI button with its build-in OnClickEvent in Unity, but it doesn't seem to work since DebugLog messages are not being written either.
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();
}
}
Upvotes: 1
Views: 3199
Reputation: 170
The advice here is correct - just select it from the available functions.
For those who have struggled to do a camera flipping script with Unity/Vuforia, here is a working example of the entire script:
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();
}
}
Upvotes: 0