Snaparazzi/Assets/Scripts/CameraManager.cs
2024-01-27 15:22:45 +01:00

70 lines
1.9 KiB
C#

using UnityEngine;
using UnityEngine.UI;
public class CameraManager : MonoBehaviour
{
public WebCamTexture webCamTexture;
public Button freezeButton;
public Button resumeButton;
// Start is called before the first frame update
void Start()
{
webCamTexture = new WebCamTexture();
//gameObject.transform.rotation = transform.rotation * Quaternion.AngleAxis(webCamTexture.videoRotationAngle, Vector3.up);
WebcamResume();
}
// Update is called once per frame
void Update()
{
var x = 0;
var y = 0;
var size = webCamTexture.height;
if (webCamTexture.height > webCamTexture.width)
{
size = webCamTexture.width;
y = (webCamTexture.height - webCamTexture.width) / 2;
}
else if (webCamTexture.height < webCamTexture.width)
{
size = webCamTexture.height;
x = (webCamTexture.width - webCamTexture.height) / 2;
}
Texture2D cropped = new(size, size);
cropped.SetPixels(webCamTexture.GetPixels(x, y, size, size));
cropped.Apply();
gameObject.GetComponent<RawImage>().texture = cropped;
gameObject.GetComponent<RawImage>().material.mainTexture = cropped;
}
public void WebcamResume()
{
webCamTexture.Play();
freezeButton.gameObject.SetActive(true);
resumeButton.gameObject.SetActive(false);
}
public void WebcamChange()
{
foreach (WebCamDevice webCamDevice in WebCamTexture.devices)
{
if (webCamTexture.deviceName != webCamDevice.name)
{
webCamTexture = new WebCamTexture(webCamDevice.name);
WebcamResume();
}
}
}
public void WebcamStop()
{
webCamTexture.Stop();
freezeButton.gameObject.SetActive(false);
resumeButton.gameObject.SetActive(true);
}
}