AndroidPermission.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using SC.XR.Unity;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. using UnityEngine.Android;
  7. public class AndroidPermission : AndroidPluginBase
  8. {
  9. private static AndroidPermission Instant;
  10. public static AndroidPermission getInstant {
  11. get {
  12. if(Instant == null) {
  13. Instant = new AndroidPermission();
  14. }
  15. return Instant;
  16. }
  17. }
  18. private AndroidJavaClass AndroidPermissionClass { get; set; }
  19. private AndroidJavaObject AndroidPermissionObject { get; set; }
  20. private AndroidPermission() {
  21. AndroidPermissionClass = GetAndroidJavaClass("com.example.libpermission.PermissionRequest");
  22. AndroidPermissionObject = ClassFunctionCallStatic<AndroidJavaObject>(AndroidPermissionClass, "getInstant", CurrentActivity);
  23. }
  24. void RequestPermission(string[] permissionList) {
  25. ObjectFunctionCall2(AndroidPermissionObject, "RequestPermission", permissionList);
  26. }
  27. string[] permissionArray;
  28. public void GetPermission(bool Camera = true, bool ExternalStorageRead = true, bool ExternalStorageWrite = true, bool Microphone = false, bool FineLocation = false, bool CoarseLocation = false) {
  29. List<string> permissionList = new List<string>();
  30. if(Camera) {
  31. permissionList.Add(Permission.Camera);
  32. //Debug.Log("PermissionRequest :Camera");
  33. }
  34. if(ExternalStorageRead) {
  35. permissionList.Add(Permission.ExternalStorageRead);
  36. //Debug.Log("PermissionRequest :ExternalStorageRead");
  37. }
  38. if(ExternalStorageWrite) {
  39. permissionList.Add(Permission.ExternalStorageWrite);
  40. //Debug.Log("PermissionRequest :ExternalStorageWrite");
  41. }
  42. if(Microphone) {
  43. permissionList.Add(Permission.Microphone);
  44. //Debug.Log("PermissionRequest :Microphone");
  45. }
  46. if(FineLocation) {
  47. permissionList.Add(Permission.FineLocation);
  48. //Debug.Log("PermissionRequest :FineLocation");
  49. }
  50. if(CoarseLocation) {
  51. permissionList.Add(Permission.CoarseLocation);
  52. //Debug.Log("PermissionRequest :CoarseLocation");
  53. }
  54. permissionArray = permissionList.ToArray();
  55. if(permissionArray.Length > 0) {
  56. RequestPermission(permissionArray);
  57. }
  58. }
  59. public void GetPermission(List<string> permissionList) {
  60. permissionArray = permissionList.ToArray();
  61. if(permissionArray.Length > 0) {
  62. RequestPermission(permissionArray);
  63. }
  64. }
  65. }