Skip to content

Commit

Permalink
fix(example): request user permission before searching webcam devices
Browse files Browse the repository at this point in the history
  • Loading branch information
homuler committed Jan 30, 2021
1 parent 0bf2bdd commit 9954a52
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,18 +1,47 @@
using System.Linq;

using System.Collections;
using UnityEngine;
using UnityEngine.UI;

#if UNITY_ANDROID
using UnityEngine.Android;
#endif

public class WebCamDeviceSelectorController : MonoBehaviour {
private GameObject sceneDirector;
private WebCamDevice[] devices;

void Start() {
IEnumerator Start() {
sceneDirector = GameObject.Find("SceneDirector");

var webCamDeviceSelector = GetComponent<Dropdown>();
webCamDeviceSelector.onValueChanged.AddListener(delegate { OnValueChanged(webCamDeviceSelector); });

#if UNITY_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Camera)) {
Permission.RequestUserPermission(Permission.Camera);
yield return new WaitForSeconds(0.1f);
}
#elif UNITY_IOS
if (!Application.HasUserAuthorization(UserAuthorization.WebCam)) {
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
}
#endif

#if UNITY_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Camera)) {
Debug.LogWarning("Not permitted to use Camera");
yield break;
}
#elif UNITY_IOS
if (!Application.HasUserAuthorization(UserAuthorization.WebCam)) {
Debug.LogWarning("Not permitted to use WebCam");
yield break;
}
#endif

yield return new WaitForEndOfFrame();

ResetOptions(WebCamTexture.devices);
}

Expand Down
19 changes: 0 additions & 19 deletions Assets/MediaPipe/Examples/Scripts/WebCamScreenController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
using System.Collections;
using UnityEngine;

#if UNITY_ANDROID
using UnityEngine.Android;
#endif

public class WebCamScreenController : MonoBehaviour {
[SerializeField] int Width = 640;
[SerializeField] int Height = 480;
Expand Down Expand Up @@ -38,27 +34,12 @@ public class WebCamScreenController : MonoBehaviour {
}

public IEnumerator ResetScreen(WebCamDevice? device) {
#if UNITY_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Camera)) {
Permission.RequestUserPermission(Permission.Camera);
}
#elif UNITY_IOS
if (!Application.HasUserAuthorization(UserAuthorization.WebCam)) {
yield return Application.RequestUserAuthorization(UserAuthorization.WebCam);
}
#endif

if (isPlaying) {
webCamTexture.Stop();
webCamTexture = null;
pixelData = null;
}

if (!Application.HasUserAuthorization(UserAuthorization.WebCam)) {
Debug.LogWarning("Not permitted to use Camera");
yield break;
}

if (device is WebCamDevice deviceValue) {
webCamDevice = deviceValue;
} else {
Expand Down

0 comments on commit 9954a52

Please sign in to comment.