facebook twitter hatena line email

Unity/GooglePlayGames

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

GooglePlayGamesとは

Unityで、GooglePlay認証を使うためのプラグイン

公式:https://github.com/playgameservices/play-games-plugin-for-unity/releases

公式マニュアル:https://firebase.google.com/docs/auth/unity/play-games?hl=ja

2025/1現在は、v2.0.0が最新

開発時にも署名が必要っぽい

公式マニュアルにあるとおり、

APK への署名は、ゲームの公開時だけでなく、ゲームの開発時にも必要です。

GooglePlayGameのアカウント切り替え

  1. GooglePlayGameアプリをストアからDLして、プロフィールを開いてアカウントを追加する
  2. GooglePlayGameアプリの設定/データ/ゲームのアカウントの変更を開いて、ゲームごとのアカウントを設定する

インストール

v.2.0.0の場合は、DL後、 current-build/GooglePlayGamesPlugin-2.0.0.unitypackageを、unityへimport

v.0.10.12の場合は、DL後、 current-build/GooglePlayGamesPlugin-0.10.12.unitypackageを、unityへimport

PlayGameプラグインバージョンアップ

0.10.12から0.11.01に上げると以下エラーが出る

error CS1061: 'PlayGamesPlatform' does not contain a definition for 'SignOut' and no accessible extension method 'SignOut' accepting a first argument of type 'PlayGamesPlatform' could be found (are you missing a using directive or an assembly reference?)

error CS1061: 'PlayGamesPlatform' does not contain a definition for 'GetServerAuthCode' and no accessible extension method 'GetServerAuthCode' accepting a first argument of type 'PlayGamesPlatform' could be found (are you missing a using directive or an assembly reference?)

error CS0246: The type or namespace name 'PlayGamesClientConfiguration' could not be found (are you missing a using directive or an assembly reference?)

error CS0117: 'PlayGamesPlatform' does not contain a definition for 'InitializeInstance'

firebaseのバージョンが11.8.0の場合

PlayGameは0.10.12を使わないと、play-sevice周りのライブラリのバージョンが合わなくてエラーが出る。

play管理画面設定

  1. play.google.com/consoleで、ユーザを増やす/PlayGamesサービスを設定する
  2. Google Cloud Platformで、APIとサービス/OAuth 同意画面を作成する→外部ユーザで作る。承認済みドメインはfirebaseのurlとか?使う。(例:hoge-xxxx.firebaseapp.com)
  3. play.google.com/consoleの、テストとリリース/設定/アプリの署名/アップロード鍵の証明書のSHA-1証明書のフィンガープリントをコピー
  4. Google Cloud Platformで、APIとサービス/認証情報から上部の認証情報を作成を選択、OAuthクライアントを選択→Androidを選択→上のSHA-1証明書のフィンガープリントを貼り付け→jsonダウンロード(クライアントidがある)
  5. Google Cloud Platformで、APIとサービス/認証情報から上部の認証情報を作成を選択、OAuthクライアントを選択→ウェブアプリケーションを選択→jsonダウンロード(クライアントidとクライアントシークレットがある)
  6. play.google.com/consoleで、ユーザを増やす/PlayGamesサービスのAndroidとゲームサーバに、上記クライアントを、それぞれ登録する
  7. play.google.com/consoleのPlayGameの[テスター] ページで、Play Store にゲームをリリースする前に、ゲームにログインできるようにするユーザーのメールアドレスを追加。

play.google.com/consoleにクラアントが出ない

登録直下はクラアントがでないので、20分ぐらい待って更新押したりすると、クライアントが選択できるようになる。

コマンドでSHA取得

keytool -exportcert -list -v -alias release_aleas_hoge1 -keystore /d/src/android/release_hoge1.keystore

公開設定

  1. play.google.com/consoleのPlayGameの公開ページで、エラーメッセージが出る項目を登録しておく。

設定

unityのGamePlayの初期設定

Assets/GooglePlayGames/GameInfo.cs

public const string ApplicationId = ""; // Filled in automatically
public const string IosClientId = "__IOS_CLIENTID__"; // Filled in automatically
public const string WebClientId = ""; // Filled in automatically
public const string NearbyConnectionServiceId = "";

上のClientIdなどをいれる。

Google Play GamesのAndroid Setupの設定

公式:https://firebase.google.com/docs/auth/unity/play-games?hl=ja

  1. unityメインメニューから[Window] > [Google Play Games] > [Setup] > [Android Setup] を開き、
  2. Directory to save Constantに、Assets/GooglePlayGamesを入れる
  3. WebAppClientIdに、上の項目で入れた、ウェブアプリケーションのClientIdをいれる。

Assets/GooglePlayGames/Plugins/Android/GooglePlayGamesManifest.plugin/AndroidManifest.xml に APP_IDが入ることを確認。

AndroidSetup実行中に"Android SDK Not found"エラーが発生したとき

Android SDK Not found
The Android SDK path was not found. Please configure it in the Unity preferences window (under External Tools).

UnityメインメニューのUnity/Settings/ExternalTools/AndroidSDKのrecommendチェックを外して、同じpathを欄に入れると、表示されなくなった。

androidビルドしたときに"Google Play Games not configured!"以下エラー

エラー詳細

Google Play Games not configured!
Warning!! Google Play Games was not configured, Game Services will not work correctly.

AndroidSetupがうまくいってないので、↑の項目を確認する。

"Error SignInRequest Sign-in failed with status code: 10"エラーが発生するとき

PlayConsoleのアップロード鍵の証明書が表示されてるか確認する。

apkを上げてなければ、署名付きのapkをupして、アップロード鍵の証明書が表示されるか確認する

FirebaseAuthで連携する

  1. firebaseAuthのログイン情報のプロバイダにPlayGameを追加し、↑のウェブアプリケーションのクライアントIDとシークレットを登録する
  2. FirebaseのプロジェクトAndroidのSHA 証明書フィンガープリントに↑のフィンガープリントを追加

サンプル実行

PlayGameログインまで設定

初期設定

using GooglePlayGames;
using GooglePlayGames.BasicApi;
using UnityEngine.SocialPlatforms;
using System.Threading.Tasks;

PlayGamesClientConfiguration config = new PlayGamesClientConfiguration.Builder()
    .RequestServerAuthCode(false /* Don't force refresh */)
    .Build();

PlayGamesPlatform.InitializeInstance(config);
PlayGamesPlatform.Activate();

ログインボタンを押したときのログイン成功処理

Social.localUser.Authenticate((bool success) => {
  // handle success or failure
});

Authenticate実行で"APP ID IS NOT CORRECTLY CONFIGURED TO USE GOOGLE PLAY GAME SERVICES"エラー

PlayConsoleのテスターにandroidアカウントのメアドを登録するとよい。

fingerprintが一致しないエラー

エラー詳細

**** APP NOT CORRECTLY CONFIGURED TO USE GOOGLE PLAY GAME SERVICES
**** DEVELOPER_ERROR
**** This is usually caused by one of these reasons:
**** (1) Your package name and certificate fingerprint do not match
****     the client ID you registered in Developer Console.
**** (2) Your App ID was incorrectly entered.
**** (3) Your game settings have not been published and you are
****     trying to log in with an account that is not listed as
****     a test account.
**** (4) A server auth code was requested, but an incorrect client
****     id was provided. The client id for server auth codes should
****     be the client id for the game server (not the android app).

解決方法:playconsoleにアップロード鍵の証明書が登録されているか確認する。なければリリース用apkを追加して、鍵を追加する。

GetServerAuthCode取得

Social.localUser.Authenticate((bool success) => {
  if (success) {
    string authCode = PlayGamesPlatform.Instance.GetServerAuthCode();
  }
});

authCodeは以下のようなフォーマットとなる

4/0AanRRruZXdYF-oq0ER57XxOgo9lBpHoNKAxdVPmYeiXIA9gI4zJpK8OSfAowK88D2JGxxx

successが失敗して、Error16がでる場合

エラー詳細

Error SignInRequest Setting result error status code to: 16

解決方法

playconsoleのplaygameのテスター数からメアドを登録すると通る。

PlayGameのFirebaseユーザ認証

Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
Firebase.Auth.Credential credential = Firebase.Auth.PlayGamesAuthProvider.GetCredential(authCode);
auth.SignInAndRetrieveDataWithCredentialAsync(credential).ContinueWith(task => {
if (task.IsCanceled)
{
    Debug.LogError("SignInAndRetrieveDataWithCredentialAsync was canceled.");
    return;
}
if (task.IsFaulted)
{
    Debug.LogError("SignInAndRetrieveDataWithCredentialAsync encountered an error: " + task.Exception);
    return;
}

Firebase.Auth.AuthResult result = task.Result;
Debug.LogFormat("User signed in successfully: {0} ({1})",
    result.User.DisplayName, result.User.UserId);
});

出力

User signed in successfully: hoge1 (pGb9NkG1Q3ZqURJQGLxvXaYXxxxx)
  • firebaseのauthにユーザレコードが追加される。idはPlayGame名が追加され、ユーザーuidは、上のカッコ内の28文字ハッシュが追加される。

その他気づき

  • PlayGame名に日本語名を入れてもそれが、idに入る。
  • firebase登録後にPlayGameの名前を変えたときは、そのゲームにログインしてもFirebase側のidは変わらない。連携解除もされない。

PlayGameのFirebaseユーザ取得

Firebase.Auth.FirebaseUser user = auth.CurrentUser;
if (user != null && user.IsValid())
{
    string playerName = user.DisplayName;

    // The user's Id, unique to the Firebase project.
    // Do NOT use this value to authenticate with your backend server, if you
    // have one; use User.TokenAsync() instead.
    string uid = user.UserId;
    Debug.Log("playerName=" + playerName);
    Debug.Log("uid=" + uid);
}

出力

playerName=
uid=AgGbeT2LIeXWUIRk0uYbeYd4uExx

PlayGameのログアウト

Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance;
auth.SignOut();

ログ出力

Debug FirebaseAuth Notifying id token listeners about a sign-out event.
Debug FirebaseAuth Notifying auth state listeners about a sign-out event.
Info TRuntime.CctTransportBackend Making request to: ttps://firebaselogging-pa.googleapis.com/v1/firelog/legacy/batchlog
Info TRuntime.CctTransportBackend Status Code: 200

PlayGameとEmailが連携した場合

firebaseのauthのユーザレコードに1行レコードがあり、PlayGameアイコンとEmailアイコンが並ぶ。

PlayConsole公開用アプリでPlayGame認証する場合

公開前であれば、アップロード鍵の証明書のsha1を以下3つに入れればよかったが、

  • FirebaseのプロジェクトAndroidのSHA 証明書フィンガープリント
  • Google Cloud Platformで、APIとサービス/認証情報から上部の認証情報を作成を選択、OAuthクライアントを選択→Androidを選択→上のSHA-1証明書のフィンガープリントを貼り付け
  • play.google.com/consoleで、ユーザを増やす/PlayGamesサービスのAndroid

PlayConsole経由でDLする場合は、アプリ署名鍵の証明書のsha1を上の3つにいれる必要がある。