Unity 根据场景请求权限弹窗
" />

您现在的位置是:首页 > 前端 > UnityUnity

Unity中安卓权限、启动安卓权限弹窗

Tdou2022-02-16【Unity】人已围观

简介Unity启动跳过权限弹窗 在AndroidManifest.xml 中application 中添加
Unity 根据场景请求权限弹窗

    public static class AndroidPermission
    {  
      /*
         启动跳过权限弹窗 在AndroidManifest.xml 中application 中添加
         <meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />
      */
      
        // 权限列表 https://www.tdouplus.com/android/153.html
        private static readonly List<string> permissions = new List<string>()
        {
            // "android.permission.INTERNET",
            // "android.permission.READ_PHONE_STATE",
            // "android.permission.ACCESS_NETWORK_STATE",
            // "android.permission.ACCESS_WIFI_STATE",
            // "android.permission.ACCESS_FINE_LOCATION",
        };

        public static void Append(string[] _permissions)
        {
            for (var i = 0; i < _permissions.Length; i++)
            {
                if (permissions.IndexOf(_permissions[i]) == -1)
                {
                    permissions.Add(_permissions[i]);
                }
            }
        }

        public static bool HasPermissions()
        {
            for (var i = 0; i < permissions.Count; i++)
            {
                bool result = HasPermission(permissions[i]);
                if (!result)
                {
                    return false;
                }
            }

            return true;
        }

        public static void RequestPermissions()
        {
            for (var i = 0; i < permissions.Count; i++)
            {
                bool result = HasPermission(permissions[i]);
                if (!result)
                {
                    RequestPermission(permissions[i]);
                }
            }
        }

        public static void RequestPermissions(string[] _permissions)
        {
            for (var i = 0; i < _permissions.Length; i++)
            {
                bool result = HasPermission(_permissions[i]);
                if (!result)
                {
                    RequestPermission(_permissions[i]);
                }
            }
        }

        public static bool HasPermission(string permission)
        {
#if UNITY_ANDROID &&!UNITY_EDITOR
                return UnityEngine.Android.Permission.HasUserAuthorizedPermission(permission);
#else
            return true;
#endif
        }

        public static void RequestPermission(string permission)
        {
#if UNITY_ANDROID &&!UNITY_EDITOR
                UnityEngine.Android.Permission.RequestUserPermission(permission);
#endif
        }
    }
    

Tags:

很赞哦! ()

  • 微信收款码
  • 支付宝收款码
打赏

文章评论

站点信息

  • 建站时间:2019-12-11
  • 网站程序:帝国CMS7.5
  • 主题模板《今夕何夕》
  • 文章统计83篇文章
  • 标签管理标签云
  • 统计数据百度统计
  • 微信公众号:扫描二维码,关注我们