Documentation

​
​

Development

User Acquisition

Monetization

Industry

  1. Unity In-App Purchasing
  2. Direct-to-Consumer (D2C) payment providers

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 2 minutes
Last updated 2 months 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:
Integrate D2C payment providers
Return to the Integrate D2C payment providers with IAP workflow page.
Fetch your Remote Catalog
Proceed to the next step in the workflow to set up your D2C payment provider.

Copyright © 2026 Unity Technologies
LegalPrivacy PolicyCookiesDocumentation Terms of UseDo Not Sell or Share My Personal InformationYour Privacy Choices (Cookie Settings)

"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S and elsewhere (more info here). Other names or brands are trademarks of their respective owners.

Some pages are machine-translated for convenience, and may contain inaccuracies. In the event of conflicting information, the English version is authoritative.

  • On this page
    • Initialize services example script

    • Next steps


Report a problem with this page