ドキュメント

サポート

Authentication

Open Unity Dashboard

Authentication

Google

Provide a Google sign-in option to enable players to authenticate using their Google accounts in your game.
読み終わるまでの所要時間 2 分最終更新 1ヶ月前

最小 SDK バージョン: 2.0.0
ここでは、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 サインインを設定する

ノート: Google サインインは、Play Games Services v1 SDK を使用している Google Play Games plugin for Unity v10.14 以前でサポートされています。
  1. Google Play Games plugin for Unity v10.14 をダウンロードし、インポートします。
  2. アプリケーションを設定して Google サインインを有効にします。Google サインインを有効にするための ゲームの設定 方法については、Google のドキュメントに従ってください。
  3. Unity Authentication について、ID プロバイダーを Google に設定します。
    1. Unity エディターメニューで、Edit (編集) > Project Settings... (プロジェクト設定...) を選択し、ナビゲーションメニューから Services (サービス) > Authentication を選択します。
    2. ID Providers (ID プロバイダー) を Google に設定し、Add (追加) を選択します。
    3. ウェブアプリケーションクライアント IDClient ID テキストフィールドに入力し、Save (保存) を選択します。
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 アカウントにリンクできます。
  1. SignInAnonymouslyAsync
    を使用して、キャッシュされたプレイヤーのアカウントにサインインします。
  2. 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); }}