Initialize services to handle the startup sequence
Initialize Unity Gaming Services, Authentication and In-App Purchasing (IAP) so you can integrate with a Direct to Consumer (D2C) payment provider.
Read time 1 minuteLast updated 13 hours ago
Create a script to initialize Unity Gaming Services (UGS) to handle the startup sequence for IAP. This startup script can do the following:
- Initialize Unity Gaming Services.
- Set up Authentication event handlers.
- Sign in.
- Initialize IAP and fetch the Remote Catalog. For more information, refer to Fetch your Remote Catalog.
In your initialization script, ensure you assign the
PurchaseManagerPurchaseManagerInitialize services example script
The following example script is namedServiceOrchestratorusing 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. Initialize Unity Gaming Services await Unity.Services.Core.UnityServices.InitializeAsync(); // 2. Setup Auth Event Handlers SetupEvents(); // 3. Sign In await SignUpAnonymouslyAsync(); // 4. Initialize IAP and Fetch Catalog await purchaseManager.InitializeIAP(); } catch (Exception 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 ex) { // Compare error code to AuthenticationErrorCodes Debug.LogException(ex); } catch (RequestFailedException ex) { // Compare error code to CommonErrorCodes Debug.LogException(ex); } }}