Documentation

In-App Purchasing

Client API

SDK API

In-App Purchasing

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:
  1. Initialize Unity Gaming Services.
  2. Set up Authentication event handlers.
  3. Sign in.
  4. Initialize IAP and fetch the Remote Catalog. For more information, refer to Fetch your Remote Catalog.
Note
Set your environment before you initialize UGS.
In your initialization script, ensure you assign the
PurchaseManager
component in the Inspector. The
PurchaseManager
component is created as part of the next fetch your Remote Catalog step.

Initialize services example script

The following example script is named
ServiceOrchestrator
:
Important
The recommended best practice is to not use anonymous sign-in as your authentication method, because this wouldn't persist purchases after the session token is lost. If frictionless platform-specific providers are not viable for your app, refer to anonymous authentication and linking.
using 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); } }}

Next steps

This page is part of a workflow to set up D2C payment providers with IAP. To continue this workflow, choose one of the following options: