其他分享
首页 > 其他分享> > Unity2019中的android动态申请权限(Permissions)

Unity2019中的android动态申请权限(Permissions)

作者:互联网

目录

 

1.先上最终研究成果(unity已经提供了面向Android的 权限申请方法)

2.疑难问题

3.原理解析:


1.先上最终研究成果(unity已经提供了面向Android的 权限申请方法)

ex:UnityEngine.Android.Permission.RequestUserPermission(s);

重要提示:使用unity的动态权限申请可以动态的弹出是否同意该权限界面,方便了很多


    string[] strs=new string[] {
        "android.permission.INTERNET",
        "android.permission.READ_PHONE_STATE",
        "android.permission.READ_EXTERNAL_STORAGE",
        "android.permission.WRITE_EXTERNAL_STORAGE",
        "android.permission.ACCESS_WIFI_STATE",
        "android.permission.ACCESS_NETWORK_STATE",
        "android.permission.GET_TASKS",
        "android.permission.REQUEST_INSTALL_PACKAGES",
        "android.permission.WAKE_LOCK",
        "android.permission.SYSTEM_ALERT_WINDOW",
        "android.permission.CHANGE_WIFI_STATE",
        "android.permission.CHANGE_NETWORK_STATE",
        "android.permission.ACCESS_COARSE_LOCATION",
        "android.permission.ACCESS_FINE_LOCATION",
        "android.permission.SYSTEM_OVERLAY_WINDOW",
        "android.permission.ACCESS_COARSE_UPDATES",
        "android.permission.WRITE_SETTINGS",
        "android.permission.BATTERY_STATS",
        "android.permission.MOUNT_UNMOUNT_FILESYSTEMS"
    }; 

    // Use this for initialization
    void Start () {
        Debug.Log("start main");
        // var iscon= PlayerConnection.instance.isConnected;

        strs.ToList().ForEach(s=>{
            //Debug.Log("RequestUserPermission: "+s);
            //if (!Permission.HasUserAuthorizedPermission(s))
            //{
                Permission.RequestUserPermission(s);
          
                Debug.Log("add RequestUserPermission: " + s);
            //}
            //else
            //{
            //    Debug.Log("it has RequestUserPermission: " + s);
            //}
        });
      


         //UnityEngine.Networking.PlayerConnection.PlayerConnection.
         //实例化上下文环境
        // adUnionContext = AdUnionContext.GetInstance();
        ////初始化SDK
        //AdUnionSDK.Init(APP_ID, new OnAuInitListenerProxy()); 

        ////初始化unity自带的控件
        initComponents();

    } 

2.疑难问题

中间遇到的问题,配置在AndroidManifest.xml中的权限有时候会导致不能动态申请到所需权限,所以可以全部注释掉,然后动态添加。

 

 

3.原理解析:

其实调用的是Android方法

com.example.androidpermissionmgr.PermissionProvider 中的verifyPermissions方法

   
  #if UNITY_ANDROID
        private AndroidJavaClass _request;
        public  AndroidJavaClass Request {
            get {
                if (_request == null)
                    _request = new AndroidJavaClass ("com.example.androidpermissionmgr.PermissionProvider");
                return _request;
            }
        }
        #else

 public void RequestPermissions (int requestCode, params string[]permissions)
        {
            Debug.Log ("[PermissionProvider] Requesting permissions: "+ requestCode + " " + permissions );
            Request.CallStatic ("verifyPermissions", gameObject.name, requestCode, permissions);
        }

 

标签:Log,permission,RequestUserPermission,Debug,Unity2019,权限,android,Permissions
来源: https://blog.csdn.net/osuckseed/article/details/101302796