Steam
Provide a Steam sign-in option to enable players to authenticate using their Steam accounts in your game.
読み終わるまでの所要時間 2 分最終更新 1ヶ月前
最小 SDK バージョン: 2.0.0
このセクションでは、Steam アカウントを使用してゲーム内でのプレイヤーの認証を設定する以下のシナリオについて説明します。- Steam アカウントサインインを設定する。
- 戻ってきたプレイヤーをサインインする、または新しいプレイヤーを作成する。
- 匿名ログインから Steam アカウントを介したプラットフォームログインへとプレイヤーを更新する。
Steam アカウントサインインの設定
- アプリケーションを作成し、Steamworks のドキュメント に従ってアプリケーション ID をメモします。
- ウェブ API キーを使用した認証 のドキュメントに従って、パブリッシャーウェブ API キーを作成します。
-
Unity 用に SDK をインストールするための手順 に従って、SteamWorks.Net SDK をインストールします。
- SteamWorks.Net SDK をプロジェクトの Assets/ フォルダーにコピーします。
- Unity プロジェクトのルートにある steam_appid.txt ファイルを開き、480 を自分のアプリケーション ID に置き換えます。
- シーン内のゲームオブジェクトに Steam Manager ゲームコンポーネントを追加します。これにより、Steam ライブラリが初期化されます。
- テストを行う前に、Steam がインストールされ、サインインされていることを確認してください。Steam ユーザーのライブラリには、開発中のゲームが表示されます。
-
Unity Authentication について、ID プロバイダーを Steam に設定します。
- Unity エディターメニューで、Edit (編集) > Project Settings... (プロジェクト設定...) を選択し、ナビゲーションメニューから Services (サービス) > Authentication を選択します。
- ID Providers (ID プロバイダー) を Steam に設定し、Add (追加) を選択します。
- App ID テキストフィールドにアプリケーション ID を入力します。
- Key テキストフィールドにパブリッシャーウェブ API キーを入力し、Save (保存) を選択します。
- 以下のサンプルコードを使用して Steam サインインを実装します。GetAuthTicketForWebApi のドキュメントを参照してください。Unity Authentication SDK は Steam セッションチケットのみを受け入れます。暗号化されたアプリケーションチケットは受け入れられません。
Callback<GetTicketForWebApiResponse_t> m_AuthTicketForWebApiResponseCallback;string m_SessionTicket;string identity = "unityauthenticationservice"void SignInWithSteam(){ // It's not necessary to add event handlers if they are // already hooked up. // Callback.Create return value must be assigned to a // member variable to prevent the GC from cleaning it up. // Create the callback to receive events when the session ticket // is ready to use in the web API. // See GetAuthSessionTicket document for details. m_AuthTicketForWebApiResponseCallback = Callback<GetTicketForWebApiResponse_t>.Create(OnAuthCallback); SteamUser.GetAuthTicketForWebApi(identity);}void OnAuthCallback(GetTicketForWebApiResponse_t callback){ m_SessionTicket = BitConverter.ToString(callback.m_rgubTicket).Replace("-", string.Empty); m_AuthTicketForWebApiResponseCallback.Dispose(); m_AuthTicketForWebApiResponseCallback = null; Debug.Log("Steam Login success. Session Ticket: " + m_SessionTicket); // Call Unity Authentication SDK to sign in or link with Steam, displayed in the following examples, using the same identity string and the m_SessionTicket.}
戻ってきたプレイヤーのサインインまたは新しいプレイヤーの作成
SignInWithSteamAsync- Steam の認証情報を使用して新しい Unity Authentication プレイヤーを作成する。
- Steam の認証情報を使用して既存のプレイヤーをサインインする。
SignInWithSteamAsyncSignInWithSteamAsyncSignInWithSteamAsyncasync Task SignInWithSteamAsync(string ticket, string identity){ try { await AuthenticationService.Instance.SignInWithSteamAsync(ticket, identity); 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); }}
プレイヤーを匿名から Steam アカウントへと更新する
匿名認証を設定した後、プレイヤーが匿名からアップグレードし、Steam アカウントを作成してサインインすることを希望する場合は、ゲームのプレイヤーにプロンプトを表示し、Steam サインインをトリガーして Steam からセッションチケットを取得するよう求める必要があります。その後、LinkWithSteamAsync API を呼び出して、プレイヤーを Steam セッションチケットにリンクします。 キャッシュされたプレイヤーが SDK に存在する場合は、キャッシュされたプレイヤーを Steam アカウントにリンクできます。- を使用して、キャッシュされたプレイヤーのアカウントにサインインします。
SignInAnonymouslyAsync - を使用して、キャッシュされたプレイヤーのアカウントを Steam アカウントにリンクします。
LinkWithSteamAsync
プレイヤーが、サインインまたは新しいプレイヤープロファイルの作成によって Steam サインインをトリガーする場合、同じ identity パラメーターを使用して Steam アクセストークンを受け取ったら、以下の API を呼び出してプレイヤーの認証を行います。async Task LinkWithSteamAsync(string ticket, string identity){ try { await AuthenticationService.Instance.LinkWithSteamAsync(ticket, identity); 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 (Exception ex) { Debug.LogError("Link failed."); Debug.LogException(ex); }}
async Task SignInWithSteamAsync(string ticket, string identity){ try { await AuthenticationService.Instance.SignInWithSteamAsync(ticket, identity); } 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); }}
戻ってきたプレイヤーをサインインするか、追加の App ID 用に新しいプレイヤーを作成する
追加の App ID (Demo、PlayTest、Alpha など) にプレイヤーをサインインするには、同じSignInWithSteamAsyncasync Task SignInWithSteamAsync(string ticket, string identity, string appId){ try { await AuthenticationService.Instance.SignInWithSteamAsync(ticket, identity, appId); 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); }}
Steam サインイン認証を実装する
Steam セッションチケットを使用して、Steam アカウントでサインインまたはリンクすることができます。プレイヤーをサインインするには、以下の API を呼び出します。SignInWithSteamAsync(string ticket, string identity)Unlink Steam account#Use the `UnlinkSteamAsync` API so your players can unlink their Steam account. Once unlinked, if their account isn’t linked to any additional identity, it transitions to an anonymous account.async Task UnlinkSteamAsync(string idToken){ try { await AuthenticationService.Instance.UnlinkSteamAsync(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); }}