Version: 2023.1
LanguageEnglish
  • C#

Permission.ShouldShowRequestPermissionRationale

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public static bool ShouldShowRequestPermissionRationale(string permission);

Parameters

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

Returns

bool The value returned by equivalent Android method.

Description

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); } } } }
Copyright © 2023 Unity Technologies
优美缔软件(上海)有限公司 版权所有
"Unity"、Unity 徽标及其他 Unity 商标是 Unity Technologies 或其附属机构在美国及其他地区的商标或注册商标。其他名称或品牌是其各自所有者的商标。
公安部备案号:
31010902002961