2024-01-27 10:26:34 +00:00
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.UI;
|
|
|
|
|
|
|
|
public class CameraManager : MonoBehaviour
|
|
|
|
{
|
2024-01-27 11:31:58 +00:00
|
|
|
public WebCamTexture webCamTexture;
|
2024-01-27 12:39:18 +00:00
|
|
|
public Button freezeButton;
|
|
|
|
public Button resumeButton;
|
2024-01-27 11:31:58 +00:00
|
|
|
|
2024-01-27 10:26:34 +00:00
|
|
|
// Start is called before the first frame update
|
2024-01-27 11:31:58 +00:00
|
|
|
void Start()
|
2024-01-27 10:26:34 +00:00
|
|
|
{
|
2024-01-27 12:39:18 +00:00
|
|
|
webCamTexture = new WebCamTexture();
|
2024-01-27 14:22:45 +00:00
|
|
|
//gameObject.transform.rotation = transform.rotation * Quaternion.AngleAxis(webCamTexture.videoRotationAngle, Vector3.up);
|
2024-01-27 12:39:18 +00:00
|
|
|
WebcamResume();
|
2024-01-27 10:26:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update is called once per frame
|
|
|
|
void Update()
|
|
|
|
{
|
2024-01-27 14:22:45 +00:00
|
|
|
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;
|
2024-01-27 10:26:34 +00:00
|
|
|
}
|
2024-01-27 12:39:18 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
2024-01-27 10:26:34 +00:00
|
|
|
}
|