Version: 2023.2

Permission.ShouldShowRequestPermissionRationale

切换到手册
public static bool ShouldShowRequestPermissionRationale (string permission);

参数

permission A string identifier for permission. This is the value of Android constant.

返回

bool The value returned by equivalent Android method.

描述

Check whether to display the UI explaining the reason for permission before requesting it.

For more information on this method, refer to Android developer documentation.

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); } } } }