起動シーケンスをハンドルするサービスの初期化
Unity Gaming Services、Authentication、アプリ内課金(IAP)を初期化して、Direct to Consumer (D2C)の支払いプロバイダーと統合できるようにします。
読み終わるまでの所要時間 1 分最終更新 8日前
IAP の起動シーケンスをハンドルするために Unity Gaming Services(UGS)を初期化するスクリプトを作成します。このスタートアップスクリプトは、以下の行うことができます。
- Unity Gaming Services を初期化します。
- Authenticationイベント ハンドラーを設定します。
- サインインします。
- IAP を初期化し、リモートカタログをフェッチします。詳細については、「 リモートカタログの取得 」を参照してください。
初期化スクリプトで、Inspector で
PurchaseManagerPurchaseManagerInitialize services スクリプト例
以下のスクリプト例は、ServiceOrchestratorusing System;using System.Threading.Tasks;using Unity.Services.Authentication;using Unity.Services.Core;using UnityEngine;// Ensure you have reference to your PurchaseManager namespace if applicablepublic class ServiceOrchestrator :MonoBehaviour{ // Assign this in the Inspector public PurchaseManager purchaseManager; async void Awake() { try { // 1.Unity Gaming Services の初期化 await Unity.Services.Core.UnityServices.InitializeAsync(); // 2.認証イベント ハンドラの設定 SetupEvents(); // 3.サインインする await SignUpAnonymouslyAsync(); // 4.IAP とフェッチカタログの初期化 await purchaseManager.InitializeIAP(); } catch (例外 e) { Debug.LogException(e); } } // Setup authentication event handlers if desired void SetupEvents() { AuthenticationService.Instance.SignedIn += () => { // Shows how to get a playerID Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}"); // Shows how to get an access token Debug.Log($"Access Token: {AuthenticationService.Instance.AccessToken}"); }; AuthenticationService.Instance.SignInFailed += (err) => { Debug.LogError(err); }; AuthenticationService.Instance.SignedOut += () => { Debug.Log("Player signed out."); }; AuthenticationService.Instance.Expired += () => { Debug.Log("Player session could not be refreshed and expired."); }; } // Sign in async Task SignUpAnonymouslyAsync() { try { await AuthenticationService.Instance.SignInAnonymouslyAsync(); Debug.Log("Sign in anonymously succeeded!"); Debug.Log($"PlayerID: {AuthenticationService.Instance.PlayerId}"); } catch (AuthenticationException など) { // Compare error code to AuthenticationErrorCodes Debug.LogException(ex); } catch (RequestFailedException など) { // Compare error code to CommonErrorCodes Debug.LogException(ex); } }}