public void Resume(OnCameraEnabled callback) { _callbackEnabled = callback; if (_webcamTexture != null) { _enabled = false; _webcamTexture.Stop(); _webcamTexture = null; } _ready = false; _enabled = true; }
public void Enable(OnCameraEnabled callback, int cameraIndex = 0) { if (_image == null) { _image = GetComponentInChildren <RawImage> (); _imageRectTransform = _image.GetComponent <RectTransform> (); } if (_aspectRatioFitter == null) { _aspectRatioFitter = GetComponentInChildren <AspectRatioFitter> (); } _callbackEnabled = callback; _cameraIndex = cameraIndex; _ready = false; _enabled = true; gameObject.SetActive(true); }
public void Disable() { _enabled = false; _cameraIndex = -1; _image.texture = null; if (_webcamTexture != null) { _webcamTexture.Stop(); _webcamTexture = null; } if (_callbackEnabled != null) { _callbackEnabled = null; } gameObject.SetActive(false); }
void Update() { if (_enabled) { if (_webcamTexture == null) { while (!Application.RequestUserAuthorization(UserAuthorization.WebCam).isDone) { return; } if (Application.HasUserAuthorization(UserAuthorization.WebCam)) { #if UNITY_EDITOR || DEVELOPMENT_BUILD Debug.Log("Webcam authorized"); #endif _webcamTexture = new WebCamTexture(WebCamTexture.devices[_cameraIndex].name); _webcamTexture.filterMode = FilterMode.Trilinear; _webcamTexture.Play(); } else { #if UNITY_EDITOR || DEVELOPMENT_BUILD Debug.Log("Webcam NOT authorized"); #endif if (_callbackEnabled != null) { _callbackEnabled.Invoke(false, "Webcam not authorized"); _callbackEnabled = null; } } } else if (_webcamTexture.isPlaying) { if (!_ready) { if (_webcamTexture.width < 100) { return; } _ready = true; if (_callbackEnabled != null) { _callbackEnabled.Invoke(true, null); _callbackEnabled = null; } } if (_webcamTexture.didUpdateThisFrame) { _aspectRatioFitter.aspectRatio = (float)_webcamTexture.width / (float)_webcamTexture.height; _imageRectTransform.localEulerAngles = new Vector3(0, 0, -_webcamTexture.videoRotationAngle); _image.texture = _webcamTexture; } } } }