最小 SDK バージョン: 2.0.0
ノート: 以下のコード サンプルが機能するためには、正しい SDK とプラグインのバージョンを使用してください。
ここでは、Google アカウントを使用してゲーム内でのプレイヤーの認証を設定する以下のシナリオについて説明します。
- Google サインインを設定する。
- 戻ってきたプレイヤーをサインインする、または新しいプレイヤーを作成する。
- 匿名ログインから Google アカウントを介したプラットフォームログインへとプレイヤーを更新する。
ゲーム内のプレイヤーに Google サインインのオプションを提供するには、Google Play Console でアプリケーションを作成し、Google Play Games plugin for Unity v10.14 をインストールしてプレイヤーをサインインし、アクセストークンを取得してください。Firebase や Google などの他のサービスに対してプレイヤーを識別するオープン認証 (ID) アクセストークンが必要です。
{
((PlayGamesLocalUser)Social.localUser).GetIdToken()
}
v10.14 以降を実行しているプラグイン バージョンについては、Google Play Games を参照してください。
ノート: 以下のコード例は、プレイヤーの Google ID トークンをすでに取得済みであることを前提としています。
Google サインインを設定する
ノート: Google サインインは、Play Games Services v1 SDK を使用している Google Play Games plugin for Unity v10.14 以前でサポートされています。
- Google Play Games plugin for Unity v10.14 をダウンロードし、インポートします。
- アプリケーションを設定して Google サインインを有効にします。Google サインインを有効にするための ゲームの設定 方法については、Google のドキュメントに従ってください。
- Unity Authentication について、ID プロバイダーを Google に設定します。
- Unity エディターメニューで、Edit (編集) > Project Settings... (プロジェクト設定...) を選択し、ナビゲーションメニューから Services (サービス) > Authentication を選択します。
- ID Providers (ID プロバイダー) を Google に設定し、Add (追加) を選択します。
- ウェブアプリケーションクライアント ID を Client ID テキストフィールドに入力し、Save (保存) を選択します。
ノート: Authentication SDK を使用するには、プラグインの設定ダイアログで ウェブアプリケーションクライアント ID を設定する必要があります。
void InitializePlayGamesLogin()
{
var config = new PlayGamesClientConfiguration.Builder()
// Requests an ID token be generated.
// This OAuth token can be used to
// identify the player to other services such as Firebase.
.RequestIdToken()
.Build();
PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.DebugLogEnabled = true;
PlayGamesPlatform.Activate();
}
void LoginGoogle()
{
Social.localUser.Authenticate(OnGoogleLogin);
}
void OnGoogleLogin(bool success)
{
if (success)
{
// Call Unity Authentication SDK to sign in or link with Google.
Debug.Log("Login with Google done. IdToken: " + ((PlayGamesLocalUser)Social.localUser).GetIdToken());
}
else
{
Debug.Log("Unsuccessful login");
}
}
戻ってきたプレイヤーのサインインまたは新しいプレイヤーの作成
SignInWithGoogleAsync
メソッドを使用して、以下のいずれかを行います。
- Google の認証情報を使用して新しい Unity Authentication プレイヤーを作成する。
- Google の認証情報を使用して既存のプレイヤーをサインインする。
async Task SignInWithGoogleAsync(string idToken)
{
try
{
await AuthenticationService.Instance.SignInWithGoogleAsync(idToken);
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 アカウントへと更新する
匿名認証を設定した後、プレイヤーが匿名からアップグレードし、Google アカウントを作成してサインインすることを希望する場合は、ゲームのプレイヤーにプロンプトを表示し、Google サインインをトリガーして Google から ID トークンを取得するよう求める必要があります。その後、LinkWithGoogleAsync
API を呼び出して、プレイヤーを Google ID トークンにリンクします。
キャッシュされたプレイヤーが SDK に存在する場合は、キャッシュされたプレイヤーを Google アカウントにリンクできます。
SignInAnonymouslyAsync
を使用して、キャッシュされたプレイヤーのアカウントにサインインします。LinkWithGoogleAsync
を使用して、キャッシュされたプレイヤーのアカウントを Google アカウントにリンクします。
キャッシュされたプレイヤーについて詳しくは、キャッシュされたプレイヤーのサインイン を参照してください。
async Task LinkWithGoogleAsync(string idToken)
{
try
{
await AuthenticationService.Instance.LinkWithGoogleAsync(idToken);
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 アカウントのリンク解除
UnlinkGoogleAsync
API を使用して、プレイヤーが自身の Google アカウントをリンク解除できるようにします。リンク解除後、そのアカウントが他のどの ID にもリンクされていない場合、そのアカウントは匿名アカウントに遷移します。
async Task UnlinkGoogleAsync(string idToken)
{
try
{
await AuthenticationService.Instance.UnlinkGoogleAsync(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);
}
}