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