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 アカウントを介したプラットフォームログインへとプレイヤーを更新する。
v10.14 以降を実行しているプラグイン バージョンについては、Google Play Games を参照してください。{ ((PlayGamesLocalUser)Social.localUser).GetIdToken()}
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 (保存) を選択します。
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- を使用して、キャッシュされたプレイヤーのアカウントにサインインします。
SignInAnonymouslyAsync - を使用して、キャッシュされたプレイヤーのアカウントを Google アカウントにリンクします。
LinkWithGoogleAsync
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 アカウントのリンク解除
UnlinkGoogleAsyncasync 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); }}