版本:Unity 6 (6000.0)
语言中文(简体)
  • C#

Permission.ShouldShowRequestPermissionRationale

建议更改

成功!

感谢帮助我们改进 Unity 文档的质量。我们无法接受所有提交,但会阅读用户提出的每条建议,并在适用情况下更新文档。

关闭

提交失败

由于某种原因,无法提交您建议的更改。请在几分钟后<a>重试</a>。感谢您抽出时间帮助我们提高 Unity 文档的质量。

关闭

取消

声明

public static bool ShouldShowRequestPermissionRationale(string permission);

参数

permission 权限的字符串标识符。这是 Android 常量的值。

返回值

bool 等效 Android 方法返回的值。

描述

检查是否要在请求权限之前显示解释权限原因的 UI。

有关此方法的更多信息,请参阅 Android 开发者文档

using UnityEngine;
using UnityEngine.Android;

public class RequestPermissionScript : MonoBehaviour { internal void PermissionCallbacks_PermissionDeniedAndDontAskAgain(string permissionName) { Debug.Log($"{permissionName} PermissionDeniedAndDontAskAgain"); }

internal void PermissionCallbacks_PermissionGranted(string permissionName) { Debug.Log($"{permissionName} PermissionCallbacks_PermissionGranted"); }

internal void PermissionCallbacks_PermissionDenied(string permissionName) { Debug.Log($"{permissionName} PermissionCallbacks_PermissionDenied"); }

void Start() { if (Permission.HasUserAuthorizedPermission(Permission.Microphone)) { // The user authorized use of the microphone. } else { bool useCallbacks = false; if (!useCallbacks) { // We do not have permission to use the microphone. // Check whether you need to display the rationale for requesting permission if (Permission.ShouldShowRequestPermissionRationale(Permission.Microphone)) { // Show a message or inform the user in other ways why your application needs the microphone permission. } // Ask for permission or proceed without the functionality enabled. Permission.RequestUserPermission(Permission.Microphone); } else { var callbacks = new PermissionCallbacks(); callbacks.PermissionDenied += PermissionCallbacks_PermissionDenied; callbacks.PermissionGranted += PermissionCallbacks_PermissionGranted; callbacks.PermissionDeniedAndDontAskAgain += PermissionCallbacks_PermissionDeniedAndDontAskAgain; Permission.RequestUserPermission(Permission.Microphone, callbacks); } } } }