Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Issue with initializing Unity IAP after Unity Services

Discussion in 'Unity IAP' started by ayseaktas, May 20, 2023.

  1. ayseaktas

    ayseaktas

    Joined:
    Jan 18, 2021
    Posts:
    50
    Hello, I'm facing an issue with initializing Unity IAP after initializing Unity Gaming Services. In the internal test, IAP works as expected. But at the start of app I am getting this warning message on logcat:
    Code (CSharp):
    1. Unity IAP: Unity In-App Purchasing requires Unity Gaming Services to have been initialized before use. - Find out how to initialize Unity Gaming Services by following the documentation https://docs.unity.com/ugs-overview/services-core-api.html#InitializationExample or download the 06 Initialize Gaming Services sample from Package Manager > In-App Purchasing > Samples. - If you are using the codeless API, you may want to enable the enable Unity Gaming Services automatic initialization by checking the Automatically initialize Unity Gaming Services checkbox at the bottom of the IAP Catalog window UnityEngine.EventSystems.StandaloneInputModule:Process()
    I am not using coddles Unity IAP but Unity Gaming Services automatic initialization enabled. This is how I initialize Unity IAP;
    Code (CSharp):
    1. using UnityEngine.Purchasing;
    2. using UnityEngine;
    3. public class GoogleIAPScript : MonoBehaviour, IStoreListener {
    4.    
    5.     private void Start() {
    6.         InitializePurchasing();
    7.     }
    8.     public void InitializePurchasing () {
    9.         if (IsInitialized()) { return; }
    10.         var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    11.         // Add product configuration
    12.         UnityPurchasing.Initialize (this, builder);
    13.     }
    14.     // Rest of the code...
    15. }
    The problem I'm facing is that Unity IAP is being initialized before Unity Gaming Services. I understand that Unity Services initialization is an asynchronous process, and I need to ensure that Unity IAP is initialized only after Unity Services is fully initialized.

    Could you please guide me on the correct way to initialize Unity IAP after Unity Gaming Services to resolve this issue?

    Thanks in advance.
     
  2. Baroni

    Baroni

    Joined:
    Aug 20, 2010
    Posts:
    3,162
    Hi,

    the checkbox for automatic initialization of Unity Gaming Services (UGS) in the Unity IAP Codeless window is actually coming from Unity IAP Codeless, which cannot be mixed with scripted Unity IAP. You will want to not use that checkbox, but initialize UGS yourself via code too.

    You can see an example on how to do it via code on this page. Ignore the environment setting, this is not required. What you want to have is "async void Start()" and in there the call to UnityServices.InitializeAsync() and then your UnityPurchasing.Initialize stuff.
     
  3. ayseaktas

    ayseaktas

    Joined:
    Jan 18, 2021
    Posts:
    50
    Problem solved. I disabled automatic initialization of Unity Gaming Services and wrote the script like this;
    Code (CSharp):
    1.  private void Start()
    2.     {
    3.         InitializeUnityServices();
    4.     }
    5.  
    6.     private async void InitializeUnityServices()
    7.     {
    8.         try
    9.         {
    10.             var options = new InitializationOptions().SetEnvironmentName(environment);
    11.             await UnityServices.InitializeAsync(options);
    12.             unityServicesInitialized = true;
    13.             InitializePurchasing();
    14.         }
    15.         catch (System.Exception exception)
    16.         {
    17.             GameAnalytics.NewErrorEvent(GAErrorSeverity.Error, "Failed to initialize Unity Gaming Services: " + exception.Message);
    18.         }
    19.     }
    20.  
    21.     public void InitializePurchasing () {
    22.         if (!unityServicesInitialized)
    23.         {
    24.             GameAnalytics.NewErrorEvent(GAErrorSeverity.Error, "Unity Gaming Services has not been initialized yet. Cannot initialize Unity IAP.");
    25.             return;
    26.         }
    27.         if (IsInitialized()) { return; }
    28.  
    29.         var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    30.         builder.AddProduct(removeAds, ProductType.NonConsumable);
    31.  
    32.         UnityPurchasing.Initialize (this, builder);
    33.     }
    34.  
    35.     private bool IsInitialized()
    36.     {
    37.         return m_StoreController != null && m_StoreExtensionProvider != null;
    38.     }
    39.  
    No warning at the start of app now. Thanks