fix: use OnEnable/OnDisable on CameraManager

This commit is contained in:
Michel Roux 2024-01-27 18:09:40 +01:00
parent cd659d87d5
commit bff9a36fce

View File

@ -11,17 +11,26 @@ public class CameraManager : MonoBehaviour
private Texture2D photo; private Texture2D photo;
// Start is called before the first frame update // Start is called before the first frame update
void OnEnable()
{
wTexture = new WebCamTexture(wDevice.name);
WebcamResume();
}
void OnDisable()
{
wTexture = null;
}
void Start() void Start()
{ {
wDevice = WebCamTexture.devices.FirstOrDefault(x => x.isFrontFacing == false); wDevice = WebCamTexture.devices.FirstOrDefault(x => x.isFrontFacing == false);
wTexture = new WebCamTexture(wDevice.name);
WebcamResume();
} }
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
gameObject.GetComponent<RawImage>().texture = photo ? photo : GetPhoto(); gameObject.GetComponent<RawImage>().texture = GetPhoto();
Resources.UnloadUnusedAssets(); Resources.UnloadUnusedAssets();
} }
@ -103,8 +112,16 @@ Texture2D CropTexture(WebCamTexture originalTexture)
return croppedTexture; return croppedTexture;
} }
Texture2D GetPhoto() public Texture2D GetPhoto()
{ {
if (photo) {
return photo;
}
if (!wTexture) {
return null;
}
Texture2D cropped = CropTexture(wTexture); Texture2D cropped = CropTexture(wTexture);
Texture2D rotated = RotateTexture(cropped, !wDevice.isFrontFacing); Texture2D rotated = RotateTexture(cropped, !wDevice.isFrontFacing);
rotated.Apply(); rotated.Apply();