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