Android 6 (API レベル 23) 以降では、Android.Permission API を使用して、カメラ、マイク、位置など、一般的に必要なシステム機能を使用するために権限をリクエストすることができます。アプリケーションの起動時ではなく、必要なときに行ないます。必要に応じて個々の権限をリクエストし、権限が必要な理由を説明するメッセージをユーザーに表示することができます。
Android が権限に使用する標準的な方法は、起動時にアプリケーションが必要とする権限のリストをユーザーに表示する方法です。理由やどのような状況で権限が使用されるかの説明はされません。これは紛らわしく、たとえアプリケーションの操作に必要であっても、ユーザーが権限を拒否する場合があります。
権限のリクエストに関する Google のガイドラインでは、ユーザーが権限のリクエストを 1 回拒否した場合は、リクエストの理由を表示し、リクエストを再度提示できるようにすることを推奨しています。
Permission.HasUserAuthorizedPermission 関数を使用して、権限がユーザーによって許可されたかどうかを確認できます。許可されない場合は、再度アプリケーションの権限が必要な理由を説明し、権限をリクエストできます。ユーザーの承認が得られたら、Permission.RequestUserPermission を呼び出して再度権限をリクエストします。この関数を呼び出すと、Android はシステム権限ダイアログを開き、ユーザーが権限を承認 (または拒否) できるようにします。
ユーザーが権限リクエストを拒否した場合は、アプリケーション内の関連する機能を無効にするか、権限が無いためにアプリケーションで機能しない場合は、ユーザーに知らせます。以前、ユーザーがシステム権限ダイアログで “Do not ask me again” オプションをチェックしている場合、 RequestUserPermission()
はシステムダイアログを開きません。この場合、ユーザーはアプリケーション権限設定に移動し、手動で権限を有効にする必要があります。
unityplayer.SkipPermissionsDialog
メタデータを Android マニフェストに加え、通常、アプリケーションの起動時に表示される権限ダイアログの表示を防ぐことができます。この場合は、保護された各機能にアクセスするために、適切なタイミングで Permission.RequestUserPermission を呼び出す必要があります。詳細は Android マニフェスト を参照してください。
Android の権限リクエストの詳細については、Android デベロッパーガイドの アプリの権限に関するおすすめの設定 を参照してください。
以下のサンプルコードは、特定の権限に対して権限が付与されているかどうかをチェックし、権限が拒否されている場合にダイアログを表示する方法を示しています。
using UnityEngine;
# if PLATFORM_ANDROID
using UnityEngine.Android;
# endif
public class MicrophoneTest : MonoBehaviour
{
GameObject dialog = null;
void Start ()
{
#if PLATFORM_ANDROID
if (!Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
Permission.RequestUserPermission(Permission.Microphone);
dialog = new GameObject();
}
#endif
}
void OnGUI()
{
#if PLATFORM_ANDROID
if(!Permission.HasUserAuthorizedPermission(Permission.Microphone))
{
//ユーザーがマイクを使用する権限を拒否しました。
//なぜそれが必要なのかを説明するメッセージを Yes/No ボタンとともに表示します。
//ユーザーが Yes と答えると、リクエストを再度表示します。
//ここにダイアログを表示します。
dialog.AddComponent<PermissionsRationaleDialog>();
return;
}
else if(dialog != null)
{
Destroy(dialog);
}
#endif
//これで、マイクを使う作業を行うことができます。
}
}
using UnityEngine;
# if PLATFORM_ANDROID
using UnityEngine.Android;
# endif
public class PermissionsRationaleDialog : MonoBehaviour
{
const int kDialogWidth = 300;
const int kDialogHeight = 100;
private bool windowOpen = true;
void DoMyWindow(int windowID)
{
GUI.Label(new Rect(10, 20, kDialogWidth - 20, kDialogHeight - 50), "Please let me use the microphone.");
GUI.Button(new Rect(10, kDialogHeight - 30, 100, 20), "No");
if (GUI.Button(new Rect(kDialogWidth - 110, kDialogHeight - 30, 100, 20), "Yes"))
{
#if PLATFORM_ANDROID
Permission.RequestUserPermission(Permission.Microphone);
#endif
windowOpen = false;
}
}
void OnGUI ()
{
if (windowOpen)
{
Rect rect = new Rect((Screen.width / 2) - (kDialogWidth / 2), (Screen.height / 2) - (kDialogHeight / 2), kDialogWidth, kDialogHeight);
GUI.ModalWindow(0, rect, DoMyWindow, "Permissions Request Dialog");
}
}
}
注意 MicrophonesTest
をコンポーネントとしてゲームオブジェクトに加えますが、PermissionsRationaleDialog
は加えません。MicrophoneTest
は自動的にゲームオブジェクトを作成し、ダイアログを表示する必要があるときに PermissionsRationaleDialog
を追加します。また、マイクの権限を追加するには、アプリケーションで Unity の Microphone クラスを使用する必要があります。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.