Binary Impact BIG
Binary Impact BIG

Reputation: 101

Android Runtime Permissions with unity 2018.3

Does the new method:

Permission.RequestUserPermission()

stop the frame cycle? Using Unity 2018.3.0f2. Method can be found in the Android namespace. Here is the link to the ducumentation: https://docs.unity3d.com/2018.3/Documentation/ScriptReference/Android.Permission.RequestUserPermission.html

Upvotes: 2

Views: 5490

Answers (3)

eAi
eAi

Reputation: 632

My experience is that this request does suspend the game, but not until after the RequestUserPermission method has returned (the next frame, I think). So this pattern doesn't work:

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

You'll have to poll HasUserAuthorizedPermission to check if you have permission.

Upvotes: 1

Binary Impact BIG
Binary Impact BIG

Reputation: 101

So i tried a lot and this here is my result in handling multiple Android Permission requests. As asked I post it here. I am using a simple Eventmanagement to display the Yes No dialog but you also can place it just there. Everything is now pretty selfexplanatory. This works for me.

To actually answer my question. The RequestUserPErmission call stops your Application.

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
}

Upvotes: 1

KYL3R
KYL3R

Reputation: 4073

No, it returns immediately. But you can wrap it in a coroutine instead of polling the result using yield return "request your permission"

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

edit: mixed them up. RequestUserPermission will return immediately as well. I guess you start that once and poll the result in Update() until it's true.

However it should not stop your game. Can you try it out and post here?

Upvotes: 0

Related Questions