Hi. Im trying to make a unity app that uses the device cameras. More specifically i want to use the wide angle back camera of my device.
This script takes all the available cameras and i have a button and switch between them. It works good, front and back. I cannot access the other wide angle back camera. My phone has 1front and 2 back cams. I can only access 1 front and 1 back. I have read and it says it takes the back cams as one object… so the solution would be to zoom out to use the other cam, like it is on my camera application on the phone. However zooming seems to be a problem too. because u need paid tools…
How can i solve my problem?
Here’s the code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PhoneCamera : MonoBehaviour
{
private bool camAvailable;
private List<WebCamTexture> allBackCams;
private int backCamCounter = 0;
private WebCamTexture backCam;
private Texture defaultBackground;
public RawImage background;
public AspectRatioFitter fit;
private void Start()
{
allBackCams = new List<WebCamTexture>();
defaultBackground = background.texture;
WebCamDevice[] devices = WebCamTexture.devices;
if(devices.Length == 0)
{
Debug.Log("No camera detected");
camAvailable = false;
return;
}
for(int i = 0; i < devices.Length;i++)
{
//if (!devices[i].isFrontFacing)
//{
backCam = new WebCamTexture(devices[i].name, Screen.width, Screen.height);
allBackCams.Add(backCam);
//}
}
if(backCam == null)
{
Debug.Log("Unable to find back camera");
return;
}
backCam.Play();
background.texture = backCam;
camAvailable = true;
}
private void Update()
{
if(!camAvailable)
{
return;
}
float ratio = (float)backCam.width / (float)backCam.height;
fit.aspectRatio = ratio;
float scaleY = backCam.videoVerticallyMirrored ? -1f : 1f;
background.rectTransform.localScale = new Vector3(1f, scaleY, 1f);
int orient = -backCam.videoRotationAngle;
background.rectTransform.localEulerAngles = new Vector3(0, 0, orient);
}
public void SwitchBackCam()
{
Debug.Log("Number of Cams: " + allBackCams.Count);
backCam = allBackCams[backCamCounter];
backCam.Play();
background.texture = backCam;
camAvailable = true;
backCamCounter++;
if(backCamCounter == allBackCams.Count)
{
backCamCounter = 0;
}
}
public void ZoomIn()
{
if (mainCamera != null)
{
mainCamera.fieldOfView -= 5f; // Adjust the zoom increment as needed
}
}
public void ZoomOut()
{
if (mainCamera != null)
{
mainCamera.fieldOfView += 5f; // Adjust the zoom increment as needed
}
}
}