diff --git a/Runtime/AvatarCreator/Scripts/UI/PhotoCaptureElement.cs b/Runtime/AvatarCreator/Scripts/UI/PhotoCaptureElement.cs index 1d14da3e..5745371e 100644 --- a/Runtime/AvatarCreator/Scripts/UI/PhotoCaptureElement.cs +++ b/Runtime/AvatarCreator/Scripts/UI/PhotoCaptureElement.cs @@ -1,5 +1,10 @@ using System.Linq; +using System.Threading; +using System.Threading.Tasks; using UnityEngine; +#if UNITY_ANDROID +using UnityEngine.Android; +#endif using UnityEngine.Events; using UnityEngine.UI; @@ -18,6 +23,11 @@ public class PhotoCaptureElement : MonoBehaviour private WebCamTexture cameraTexture; private bool isInitialized; + private int videoRotationAngle; + private bool videoVerticallyMirrored; + + private CancellationTokenSource ctxSource; + private void OnEnable() { if (initializeOnEnable) @@ -28,11 +38,14 @@ private void OnEnable() private void OnDisable() { + ctxSource?.Cancel(); StopCamera(); } - public void StartCamera() + public async void StartCamera() { + await GetPermission(); + if (!isInitialized) { InitializeCamera(); @@ -41,6 +54,14 @@ public void StartCamera() if (cameraTexture != null && !cameraTexture.isPlaying) { cameraTexture.Play(); + + var currentRotation = cameraTextureTarget.transform.rotation; + cameraTextureTarget.transform.rotation = Quaternion.Euler(currentRotation.eulerAngles.x, currentRotation.eulerAngles.y, cameraTexture.videoRotationAngle); + + if (!cameraTexture.videoVerticallyMirrored) + { + cameraTextureTarget.transform.localScale = new Vector3(-1, 1, 1); + } } } @@ -64,6 +85,35 @@ public void TakePhoto() onPhotoCaptured?.Invoke(texture); } + private async Task GetPermission() + { + ctxSource?.Cancel(); + ctxSource = new CancellationTokenSource(); + +#if UNITY_ANDROID + if (!Permission.HasUserAuthorizedPermission(Permission.Camera)) + { + Permission.RequestUserPermission(Permission.Camera); + } + + while (!Permission.HasUserAuthorizedPermission(Permission.Camera) && !ctxSource.IsCancellationRequested) + { + await Task.Yield(); + } + +#elif UNITY_IOS + if (!Application.HasUserAuthorization(UserAuthorization.WebCam)) + { + var async = Application.RequestUserAuthorization(UserAuthorization.Microphone); + while (!async.isDone && !ctxSource.IsCancellationRequested) + { + await Task.Yield(); + } + } +#endif + + } + private void InitializeCamera() { var webCamDevice = GetWebCamDevice();