From bf39b053a5474f729b4377e57bcc3d5a66d79fc6 Mon Sep 17 00:00:00 2001 From: Robin <1121080+rYuuk@users.noreply.github.com> Date: Tue, 2 Jan 2024 11:06:15 +0100 Subject: [PATCH] [SDK-593] Add permission and orientation fix to photo capture element (#192) ## [SDK-593](https://ready-player-me.atlassian.net/browse/SDK-593) ## Description - Added fetching of camera permission for ios and android - Also fixed orientation of captured image --- .../Scripts/UI/PhotoCaptureElement.cs | 52 ++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) 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();