PermissionRequest.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using SC.XR.Unity;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.Android;
  6. public class PermissionRequest : MonoBehaviour
  7. {
  8. public bool Camera = false;
  9. public bool ExternalStorageRead = false;
  10. public bool ExternalStorageWrite = false;
  11. public bool Microphone = false;
  12. public bool FineLocation = false;
  13. public bool CoarseLocation = false;
  14. static Coroutine requestC;
  15. static Dictionary<string,bool> permissionDic = new Dictionary<string, bool>();
  16. List<string> permission = new List<string>();
  17. private static PermissionRequest instance;
  18. public static PermissionRequest getInstance {
  19. get {
  20. if (instance == null) {
  21. instance = new GameObject("PermissionRequest").AddComponent<PermissionRequest>();
  22. }
  23. return instance;
  24. }
  25. }
  26. void Awake() {
  27. instance = this;
  28. }
  29. /// <summary>
  30. /// Like UnityEngine.Android.Permission.Camera
  31. /// </summary>
  32. /// <param name="permission">Like Permission.Camera</param>
  33. public void GetPerssion(string permission="") {
  34. if (permission != "" && ! permissionDic.ContainsKey(permission) && !Permission.HasUserAuthorizedPermission(permission)) {
  35. permissionDic.Add(permission,true);
  36. }
  37. if (requestC == null) {
  38. requestC = StartCoroutine(RequestPerssion());
  39. }
  40. }
  41. void OnEnable() {
  42. if (Camera) {
  43. GetPerssion(Permission.Camera);
  44. }
  45. if (ExternalStorageRead) {
  46. GetPerssion(Permission.ExternalStorageRead);
  47. }
  48. if (ExternalStorageWrite) {
  49. GetPerssion(Permission.ExternalStorageWrite);
  50. }
  51. if (Microphone) {
  52. GetPerssion(Permission.Microphone);
  53. }
  54. if (FineLocation) {
  55. GetPerssion(Permission.FineLocation);
  56. }
  57. if (CoarseLocation) {
  58. GetPerssion(Permission.CoarseLocation);
  59. }
  60. }
  61. IEnumerator RequestPerssion() {
  62. yield return new WaitForEndOfFrame();
  63. if (permissionDic.Count > 0) {
  64. permission.Clear();
  65. foreach (var item in permissionDic.Keys) {
  66. permission.Add(item);
  67. DebugMy.Log("RequestPerssion:" + item, this, true);
  68. }
  69. AndroidPermission.getInstant.GetPermission(permission);
  70. permissionDic.Clear();
  71. }
  72. requestC = null;
  73. }
  74. }