カスタム ID サインイン
最小 SDK バージョン: 3.1.0
このセクションでは、カスタムトークンを使用してゲーム内でのプレイヤーの認証を設定するための以下のシナリオについて説明します。
- カスタム ID プロバイダーサインインを設定する。
- 戻ってきたプレイヤーをサインインする、または新しいプレイヤーを作成する。
ゲーム内のプレイヤーにカスタム ID サインインのオプションを提供するには、アプリケーションを設定してカスタム 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 を呼び出します。
ゲームサーバーから取得した アクセストークン と セッショントークン を使用して、
ProcessAuthenticationTokens
API を呼び出します。
Authentication トークンの処理
ProcessAuthenticationTokens
を使用して、Unity Authentication サービスアクセストークンを処理し、プレイヤーの認証を必要とする、ゲームに統合されている他の UGS SDK で使用できるようにします。アクセストークンには期限があるため、アクセストークンを手動で更新し、新しいアクセストークンを使用して ProcessAuthenticationTokens
を呼び出す必要があります。ProcessAuthenticationTokens
の呼び出しには、アクセストークンとセッショントークンの両方を使用できます。Unity Authentication SDK は、期限が切れる前にアクセストークンを更新し、プレイヤーのセッションをアクティブに保ち、キャッシュされたプレイヤーのサインイン を有効にします。
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);
}
}
サンプルコードはメソッドのみであり、クラスではありません。