Google Play Games

最小 SDK バージョン: 2.1.0

このセクションでは、Google Play Games アカウントを使用してゲーム内でのプレイヤーの認証を設定する以下のシナリオについて説明します。

ノート: 以下のコード例は、プレイヤーのワンタイム承認コードをすでに取得済みであることを前提としています。

  • Google Play Games サインインを設定する。
  • 戻ってきたプレイヤーをサインインする、または新しいプレイヤーを作成する。
  • 匿名ログインから Google Play Games アカウントを介したプラットフォームログインへとプレイヤーを更新する。

ゲーム内のプレイヤーに Google Play Games サインインのオプションを提供するには、Google Play Console でアプリケーションを作成し、Google Play Games plugin for Unity v11.01 以上をインストールしてプレイヤーをサインインし、ワンタイム承認コードを取得してください。

プラグインバージョン v10.14 以前を使用している場合は、Google ID プロバイダー のセクションを参照してください。

Google Play Games サインインを設定する

ノート: Google Play Games サインインは Android デバイスのみと互換性があります。

ノート: Google Play Games サインインは、Google Play Games plugin for Unity v11.01 以上でサポートされています。これについては、Play Games Services v2 SDK の使用が推奨されています。

  1. Google Play Games Plugin for Unity をダウンロードし、インポートします。

  2. アプリケーションを設定して Google サインインを有効にします。Google サインインを有効にするためのゲームの設定 方法については、Google のドキュメントに従ってください。

  3. Unity Authentication について、ID プロバイダーを Google に設定します。

    1. Unity エディターメニューで、Edit (編集) > Project Settings... (プロジェクト設定...) を選択し、ナビゲーションメニューから Services (サービス) > Authentication を選択します。

    2. ID Providers (ID プロバイダー) を Google Play Services に設定し、Add (追加) をクリックします。

    3. Client ID テキストフィールドに ウェブアプリケーションクライアント ID を入力します。

    4. Client Secret (クライアントシークレット) テキストフィールドに ウェブアプリケーションクライアントシークレット を入力し、Save (保存) を選択します。

      ノート: Authentication SDK を使用するには、プラグインの設定ダイアログで ウェブアプリケーションクライアント ID を設定する必要があります。

以下のサンプルコードに従って、Google Play Games サインインを実装してください。

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 の認証情報を使用して既存のプレイヤーをサインインする。

プロジェクト内の Unity Authentication プレイヤーが認証情報と関連付けられていない場合、SignInWithGooglePlayGamesAsync は新しいプレイヤーを作成します。プロジェクト内の Unity Authentication プレイヤーが認証情報と関連付けられている場合、SignInWithGooglePlayGamesAsync はそのプレイヤーのアカウントにサインインします。この関数ではキャッシュされたプレイヤーは考慮されません。SignInWithGooglePlayGamesAsync はキャッシュされたプレイヤーを置き換えます。

async 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 API を呼び出してプレイヤーをリンクします。

キャッシュされたプレイヤーが SDK に存在する場合は、キャッシュされたプレイヤーを Google Play Games アカウントにリンクできます。

  1. SignInAnonymouslyAsync を使用して、キャッシュされたプレイヤーのアカウントにサインインします。
  2. LinkWithGooglePlayGamesAsync を使用して、キャッシュされたプレイヤーのアカウントを Google Play Games アカウントにリンクします。

キャッシュされたプレイヤーについて詳しくは、キャッシュされたプレイヤーのサインイン を参照してください。

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 アカウントのリンク解除

UnlinkGooglePlayGamesAsync API を使用して、プレイヤーが自身の Google Play Games アカウントをリンク解除できるようにします。リンク解除後、そのアカウントが他のどの ID にもリンクされていない場合、そのアカウントは匿名アカウントに遷移します。

async 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);
   }
}