カスタム ID サインイン
Provide a Custom ID sign-in option to enable players to authenticate using your custom identity system in your game.
読み終わるまでの所要時間 1 分最終更新 1ヶ月前
最小 SDK バージョン: 3.1.0
このセクションでは、カスタムトークンを使用してゲーム内でのプレイヤーの認証を設定するための以下のシナリオについて説明します。- カスタム ID プロバイダーサインインを設定する。
- 戻ってきたプレイヤーをサインインする、または新しいプレイヤーを作成する。
カスタム ID サインインの設定
-
カスタム ID を Unity 用の ID プロバイダーとして追加します。
- Unity エディターメニューで、Edit (編集) > Project Settings... (プロジェクト設定...) を選択し、ナビゲーションメニューから Services (サービス) > Authentication を選択します。
- ID Providers (ID プロバイダー) を Custom (カスタム) に設定し、Add (追加) を選択します。
- サービスアカウントを作成し、プロジェクトロール Player Authentication Token Issuer (プレイヤー認証トークン発行者) を追加します。
戻ってきたプレイヤーのサインインまたは新しいプレイヤーの作成
-
カスタム ID 認証を使用してプレイヤーをサインインさせるには、独自のゲームサーバーにリクエストを送信して、Unity Authentication Service のアクセストークン と セッショントークン を取得する必要があります。
ゲームサーバーで、以下を行います。
- トークン交換 API を呼び出して、ステートレストークンを取得します。
- カスタム ID を使用したサインインの API を呼び出します。
-
ゲームサーバーから取得した アクセストークン と セッショントークン を使用して、API を呼び出します。
ProcessAuthenticationTokens
Authentication トークンの処理
ProcessAuthenticationTokensProcessAuthenticationTokensProcessAuthenticationTokensサンプルコードはメソッドのみであり、クラスではありません。using Unity.Services.Authentication;void SignUserWithCustomTokenWithAutoRefresh(){ try { // Check if a cached player already exists by checking if the session token exists if (AuthenticationService.Instance.SessionTokenExists) { // This call will sign in the cached player. await AuthenticationService.Instance.SignInAnonymouslyAsync(); Debug.Log("Cached user sign in succeeded!"); } else { var userTokens = // Fetch the user tokens using your method calls AuthenticationService.Instance.ProcessAuthenticationTokens(userTokens.AccessToken, userTokens.SessionToken) } // Shows how to get the playerID Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}"); } 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); } catch (Exception ex) { // Handle exceptions from your method call Debug.LogException(ex); }}