统一2018.3的Android运行时权限

时间:2019-01-08 13:19:58

标签: c# android unity3d

执行新方法:

Permission.RequestUserPermission()

停止帧周期?使用Unity 2018.3.0f2。可以在Android名称空间中找到方法。这是文档翻译的链接:https://docs.unity3d.com/2018.3/Documentation/ScriptReference/Android.Permission.RequestUserPermission.html

3 个答案:

答案 0 :(得分:1)

否,它会立即返回。但是您可以将其包装在协程中,而不用使用yield return "request your permission"

轮询结果

https://docs.unity3d.com/ScriptReference/Application.RequestUserAuthorization.html

编辑:将它们混合在一起。 RequestUserPermission也将立即返回。我想您应该从头开始一次,然后在Update()中轮询结果,直到它是真的。

但是它不应该停止您的游戏。您可以尝试并在此处发布吗?

答案 1 :(得分:0)

所以我尝试了很多,这是我处理多个Android权限请求的结果。根据要求,我将其张贴在这里。我使用一个简单的Eventmanagement来显示Yes Yes对话框,但是您也可以将其放置在此处。现在一切都变得不言自明。这对我有用。

实际回答我的问题。 RequestUserPErmission调用将停止您的应用程序。

using UnityEngine;
using System.Collections.Generic;

#if PLATFORM_ANDROID
using UnityEngine.Android;
#endif

public class PermissionTest : MonoBehaviour {

    public static bool PermissionCheckInProgress = false;

    private Dictionary<string, string> m_msgTexts = new Dictionary<string, string>();

#if PLATFORM_ANDROID

    private void OnEnable() {
        App.MsgSystem.MsgSystem.MSG_CONFIRMED.AddListener( HandleAnswer );
    }

    private void OnDisable() {
        App.MsgSystem.MsgSystem.MSG_CONFIRMED.RemoveListener( HandleAnswer );
    }

    private void Start() {
        m_msgTexts.Add( Permission.Camera, "That is why i need the camera." );
        m_msgTexts.Add( Permission.FineLocation, "That is why i need the GPS." );
        m_msgTexts.Add( Permission.ExternalStorageWrite, "That is why i need the SD card." );

        //initial perission request
        foreach (KeyValuePair<string, string> entry in m_msgTexts) {
            if (!Permission.HasUserAuthorizedPermission( entry.Key )) {
                Permission.RequestUserPermission( entry.Key );
            }
        }
    }

    private void Update() {
        if (App.AppManager.Instance._settings._DatenschutzAccepted ) {
            if (!PermissionCheckInProgress) {
                foreach (KeyValuePair<string, string> entry in m_msgTexts) {
                    if (entry.Value != "done") {
                        AskForPermission( entry.Key, entry.Value );
                    }
                }
            }
        }
    }

    private void AskForPermission(string PermissionType, string msg) {
        if (!Permission.HasUserAuthorizedPermission( PermissionType ) && !PermissionCheckInProgress) {

            // The user denied permission to use the asked permission type.
            // so send a msg in our msgsystem to ask again with a description why we need it
            // then wait for the answer

            App.MsgSystem.MsgSystem.MSG_PROMPT.Dispatch( msg, PermissionType );
            PermissionCheckInProgress = true;
            m_msgTexts[ PermissionType ] = "done";
        }
    }

    /// <summary>
    /// Callback from the yes or no dialog
    /// </summary>
    /// <param name="handle"></param>
    private void HandleAnswer(string handle) {
        Permission.RequestUserPermission( handle );

        PermissionCheckInProgress = false;
    }

#endif
}

答案 2 :(得分:0)

我的经验是,该请求确实使游戏暂停,但直到RequestUserPermission方法返回(我认为是下一帧)之后才暂停游戏。因此此模式不起作用:

if (!Permission.HasUserAuthorizedPermission(...)) 
{
    Permission.RequestUserPermission(...);
    if (Permission.HasUserAuthorizedPermission(...)) 
    {
        // never happens
    }
}

您必须轮询HasUserAuthorizedPermission来检查您是否具有权限。