Giter Site home page Giter Site logo

aaditya009 / unityandroidruntimepermissions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from yasirkula/unityandroidruntimepermissions

0.0 1.0 0.0 57 KB

A native Unity plugin to handle runtime permissions on Android M+

License: MIT License

Java 43.66% C# 56.34%

unityandroidruntimepermissions's Introduction

Unity Android Runtime Permissions

runtime_permission

Available on Asset Store: https://www.assetstore.unity3d.com/en/#!/content/117803

Forum Thread: https://forum.unity.com/threads/open-source-androidruntimepermissions-manage-runtime-permissions-synchronously-on-android-m.528833/

Based on UnityAndroidPermissions (MIT License): https://github.com/Over17/UnityAndroidPermissions

This plugin helps you query/request runtime permissions synchronously on Android M and later. It also works on older Android versions and detects whether a requested permission is declared in AndroidManifest or not.

How To

Simply import RuntimePermissions.unitypackage to your project and you are good to go! There is one optional step, though: by default, Unity shows a permission dialog on startup to prevent plugins from crashing/malfunctioning. This can be disabled, if you want; but you must make sure to handle all the runtime permissions carefully in your app's lifecycle. To disable this dialog, add the following line inside the <application>...</application> tag of Plugins/Android/AndroidManifest.xml:

<meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />

NOTE: if your project doesn't have an AndroidManifest, you can copy Unity's default one from C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidPlayer (it might be located in a subfolder, like 'Apk') to Assets/Plugins/Android (credit).

You can use the following static functions of AndroidRuntimePermissions to manage runtime permissions:

Permission CheckPermission( string permission ): checks whether or not the permission is granted. Permission is an enum that can take 3 values:

  • Granted: permission is granted
  • ShouldAsk: permission is not granted yet, but we can ask the user for permission via RequestPermission function. As long as the user doesn't select "Don't ask again" while denying the permission, ShouldAsk is returned
  • Denied: we don't have permission and we can't ask the user for permission. In this case, user has to give the permission from app's Settings. This happens when user selects "Don't ask again" while denying the permission or when user is not allowed to give that permission (parental controls etc.)

Permission[] CheckPermissions( params string[] permissions ): queries multiple permissions simultaneously. The returned array will contain one Permission entry per each queried permission

Permission RequestPermission( string permission ): requests a permission from the user and returns the result. It is recommended to show a brief explanation before asking the permission so that user understands why the permission is needed and doesn't click Deny or worse, "Don't ask again"

Permission[] RequestPermissions( params string[] permissions ): requests multiple permissions simultaneously

void OpenSettings(): opens the settings for this app, from where the user can manually grant permission(s) in case a needed permission's state is Permission.Denied

Example Code

The following code requests WRITE_EXTERNAL_STORAGE permission (it must be declared in AndroidManifest) when bottom-right corner of the screen is touched:

void Update()
{
	if( Input.GetMouseButtonDown( 0 ) && Input.mousePosition.x > Screen.width * 0.8f && Input.mousePosition.y < Screen.height * 0.2f )
	{
		AndroidRuntimePermissions.Permission result = AndroidRuntimePermissions.RequestPermission( "android.permission.WRITE_EXTERNAL_STORAGE" );
		if( result == AndroidRuntimePermissions.Permission.Granted )
			Debug.Log( "We have permission to access external storage!" );
		else
			Debug.Log( "Permission state: " + result );
		
		// Requesting WRITE_EXTERNAL_STORAGE and CAMERA permissions simultaneously
		//AndroidRuntimePermissions.Permission[] result = AndroidRuntimePermissions.RequestPermissions( "android.permission.WRITE_EXTERNAL_STORAGE", "android.permission.CAMERA" );
		//if( result[0] == AndroidRuntimePermissions.Permission.Granted && result[1] == AndroidRuntimePermissions.Permission.Granted )
		//	Debug.Log( "We have all the permissions!" );
		//else
		//	Debug.Log( "Some permission(s) are not granted..." );
	}
}

unityandroidruntimepermissions's People

Contributors

yasirkula avatar

Watchers

 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.