using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CameraScript : MonoBehaviour
{
private int currentCamIndex = -1;
private WebCamTexture webCamTexture;
public RawImage rawImage;
public GameObject Image;
public void StartStopCamera()
{
if (webCamTexture != null)
{
StopCamera();
}
else
{
for (int i = 0; i < WebCamTexture.devices.Length; i++)
{
if (!WebCamTexture.devices[i].isFrontFacing)
{
currentCamIndex = i;
break;
}
}
if (currentCamIndex == -1)
return;
WebCamDevice device = WebCamTexture.devices[currentCamIndex];
if (!device.Equals(null))
{
if (!rawImage.gameObject.activeSelf)
rawImage.gameObject.SetActive(true);
Image.SetActive(false);
int width = 1920;
int height = 1080;
webCamTexture = new WebCamTexture(device.name, width, height, 24);
rawImage.texture = webCamTexture;
webCamTexture.Play();
}
}
}
public void StopCamera()
{
rawImage.texture = null;
webCamTexture?.Stop();
webCamTexture = null;
if (rawImage.gameObject.activeSelf)
rawImage.gameObject.SetActive(false);
Image.SetActive(true);
}
}