Google Play Games
Provide a Google Play Games sign-in option to provide a frictionless authentication experience for Android players.
読み終わるまでの所要時間 2 分最終更新 1ヶ月前
最小 SDK バージョン: 2.1.0
このセクションでは、Google Play Games アカウントを使用してゲーム内でのプレイヤーの認証を設定する以下のシナリオについて説明します。 ノート: 以下のコード例は、プレイヤーのワンタイム承認コードをすでに取得済みであることを前提としています。- Google Play Games サインインを設定する。
- 戻ってきたプレイヤーをサインインする、または新しいプレイヤーを作成する。
- 匿名ログインから Google Play Games アカウントを介したプラットフォームログインへとプレイヤーを更新する。
Google Play Games サインインを設定する
ノート: Google Play Games サインインは Android デバイスのみと互換性があります。 ノート: Google Play Games サインインは、Google Play Games plugin for Unity v11.01 以上でサポートされています。これについては、Play Games Services v2 SDK の使用が推奨されています。- Google Play Games Plugin for Unity をダウンロードし、インポートします。
- アプリケーションを設定して Google サインインを有効にします。Google サインインを有効にするためのゲームの設定 方法については、Google のドキュメントに従ってください。
-
Unity Authentication について、ID プロバイダーを Google に設定します。
- Unity エディターメニューで、Edit (編集) > Project Settings... (プロジェクト設定...) を選択し、ナビゲーションメニューから Services (サービス) > Authentication を選択します。
- ID Providers (ID プロバイダー) を Google Play Services に設定し、Add (追加) をクリックします。
- Client ID テキストフィールドに ウェブアプリケーションクライアント ID を入力します。
-
Client Secret (クライアントシークレット) テキストフィールドに ウェブアプリケーションクライアントシークレット を入力し、Save (保存) を選択します。
using GooglePlayGames;using GooglePlayGames.BasicApi;using UnityEngine;public class GooglePlayGamesExampleScript : MonoBehaviour{ public string Token; public string Error; void Awake() { //Initialize PlayGamesPlatform PlayGamesPlatform.Activate(); LoginGooglePlayGames(); } public void LoginGooglePlayGames() { PlayGamesPlatform.Instance.Authenticate((success) => { if (success == SignInStatus.Success) { Debug.Log("Login with Google Play games successful."); PlayGamesPlatform.Instance.RequestServerSideAccess(true, code => { Debug.Log("Authorization code: " + code); Token = code;// This token serves as an example to be used for SignInWithGooglePlayGames }); } else { Error = "Failed to retrieve Google play games authorization code"; Debug.Log("Login Unsuccessful"); } }); }}
戻ってきたプレイヤーのサインインまたは新しいプレイヤーの作成
SignInWithGooglePlayGamesAsync- Google Play Games の認証情報を使用して新しい Unity Authentication プレイヤーを作成する。
- Google Play Games の認証情報を使用して既存のプレイヤーをサインインする。
SignInWithGooglePlayGamesAsyncSignInWithGooglePlayGamesAsyncSignInWithGooglePlayGamesAsyncasync Task SignInWithGooglePlayGamesAsync(string authCode){ try { await AuthenticationService.Instance.SignInWithGooglePlayGamesAsync(authCode); Debug.Log("SignIn is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
プレイヤーを匿名から Google Play Games アカウントへと更新する
匿名認証を設定した後、プレイヤーが匿名からアップグレードし、Google Play Games アカウントを作成してサインインすることを希望する場合は、ゲームのプレイヤーにプロンプトを表示し、Google Play Games サインインをトリガーして Google からワンタイム承認コードを取得するよう求める必要があります。その後、LinkWithGooglePlayGamesAsync- を使用して、キャッシュされたプレイヤーのアカウントにサインインします。
SignInAnonymouslyAsync - を使用して、キャッシュされたプレイヤーのアカウントを Google Play Games アカウントにリンクします。
LinkWithGooglePlayGamesAsync
async Task LinkWithGooglePlayGamesAsync(string authCode){ try { await AuthenticationService.Instance.LinkWithGooglePlayGamesAsync(authCode); Debug.Log("Link is successful."); } catch (AuthenticationException ex) when (ex.ErrorCode == AuthenticationErrorCodes.AccountAlreadyLinked) { // Prompt the player with an error message. Debug.LogError("This user is already linked with another account. Log in instead."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}
Google Play アカウントのリンク解除
UnlinkGooglePlayGamesAsyncasync Task UnlinkGooglePlayGamesAsync(string idToken){ try { await AuthenticationService.Instance.UnlinkGooglePlayGamesAsync(idToken); Debug.Log("Unlink is successful."); } catch (AuthenticationException ex) { // Compare error code to AuthenticationErrorCodes // Notify the player with the proper error message Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes // Notify the player with the proper error message Debug.LogException(ex); }}