Coder Social home page Coder Social logo

unityandroidpermissions's Introduction

UnityAndroidPermissions

Starting with Android Marshmallow (Android 6), Google introduced Runtime permissions system where the user is asked to grant a permission in runtime rather than doing that during installation of the app. However, Unity for Android is not supporting it out of the box because:

  • the corresponding Android API requires an Activity, when Unity can run without it. All non-Activity Unity applications are not supported for that reason
  • the plugins may add a dangerous permission and not have the code to handle it correctly, thus causing the whole app to crash This is the reason why Unity prompts the user for all the permissions on startup. This behavior is the safest and most compatible.

NOTE: Unity 2018.3 introduces runtime permissions support and API, so starting with this version you don't need this plugin in most cases.

However, Google requires the runtime permission system to be implemented to get featured on Google Play. To let the user implement it (and take the responsibility), the Unity's dialog on startup can be suppressed by adding "unityplayer.SkipPermissionsDialog"="true" metadata tag to application or activity section of the Android Manifest.

This plugin is one of the Android runtime permissions for Unity implementations. It provides the API to check the status of a permission, request a set of permissions and get a callback with the result.

API

AndroidPermissionsManager is the class which provides you the following methods:

  • bool IsPermissionGranted(string permissionName) to check the status of a permission
  • void RequestPermission(string[] permissionNames, AndroidPermissionCallback callback) to query for an array of permissions. Pass AndroidPermissionCallback object with your own callback implementations defined in delegates (Action<string> onGrantedCallback is called when a permission is granted, Action<string> onDeniedCallback - when a permission is denied, Action<string> onPermissionDeniedAndDontAskAgainAction - when a permission is denied and the user marks "Don't ask again" checkbox, corresponding permission name is passed as a string param). NOTE: the callbacks are called from Java UI thread (which is different from Unity UI thread), so be careful about the APIs you call from the callback.

Usage

  1. Should work with Unity 5.3+. Please report an issue if it does not for you
  2. Add the plugin to your project. You need the AAR and the C# script (Assets/Plugins/Android/unityandroidpermissions.aar and Assets/Scripts/AndroidPermissionsManager.cs)
  3. Use the C# API in your scripts to check the permission status and request it if necessary, right before you actually need this permission
  4. Enjoy

For a script sample, please refer to Assets/Scripts/AndroidPermissionsUsageExample.cs.

How to Build the Plugin From Source

Run gradlew assemble from src/UnityAndroidPermissions/

See Also

Please refer to Google documentation for more details: https://developer.android.com/training/permissions/requesting.html

unityandroidpermissions's People

Contributors

over17 avatar bryksin avatar fadookie avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.