Search Unity

Question UnityPurchasing.Initialize doesn't work after initializing UGS

Discussion in 'Unity Gaming Services General Discussion' started by UDN_7a2eceda-93d6-4468-b982-388651c336e0, Nov 23, 2022.

  1. UDN_7a2eceda-93d6-4468-b982-388651c336e0

    UDN_7a2eceda-93d6-4468-b982-388651c336e0

    Joined:
    Jul 25, 2016
    Posts:
    14
    I updated the new IAP version and it said it requires initializing UGS first.
    So I added it in my code (I'm not using codeless IAP) but I got a problem with my IAP.
    my code is below.


    using UnityEngine;
    using System;
    using UnityEngine.Purchasing;
    using Unity.Services.Core;
    using Unity.Services.Core.Environments;
    public class IAPManager : MonoBehaviour, IStoreListener
    {
    private static IStoreController m_StoreController;
    // The Unity Purchasing system.
    private static IExtensionProvider m_StoreExtensionProvider;
    // The store-specific Purchasing subsystems.
    public string[] PRODUCT_PACKAGE;
    //Unity Gaming Services
    const string k_Environment = "production";
    void Awake()
    {
    // Uncomment this line to initialize Unity Gaming Services.
    Initialize(OnSuccess, OnError);
    }
    void Initialize(Action onSuccess, Action<string> onError)
    {
    try
    {
    var options = new InitializationOptions().SetEnvironmentName(k_Environment);
    UnityServices.InitializeAsync(options).ContinueWith(task => onSuccess());
    }
    catch (Exception exception)
    {
    onError(exception.Message);
    }
    }
    void OnError(string message)
    {
    }
    void OnSuccess()
    {
    Debug.Log("Success");
    // If we haven't set up the Unity Purchasing reference
    if (m_StoreController == null)
    {
    // Begin to configure our connection to Purchasing
    InitializePurchasing();
    }
    }
    public void InitializePurchasing()
    {
    // If we have already connected to Purchasing ...
    if (IsInitialized())
    {
    // ... we are done here.
    return;
    }
    // Create a builder, first passing in a suite of Unity provided stores.
    var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
    // Add a product to sell / restore by way of its identifier, associating the general identifier
    // with its store-specific identifiers.
    for (int i = 0; i < PRODUCT_PACKAGE.Length; i++)
    {
    builder.AddProduct(PRODUCT_PACKAGE, ProductType.Consumable);
    }
    // Kick off the remainder of the set-up with an asynchrounous call, passing the configuration
    // and this class' instance. Expect a response either in OnInitialized or OnInitializeFailed.
    UnityPurchasing.Initialize(this, builder);
    }
    private bool IsInitialized()
    {
    // Only say we are initialized if both the Purchasing references are set.
    return m_StoreController != null && m_StoreExtensionProvider != null;
    }
    public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
    {
    // Purchasing has succeeded initializing. Collect our Purchasing references.
    Debug.Log("OnInitialized: PASS");
    // Overall Purchasing system, configured with products for this application.
    m_StoreController = controller;
    // Store specific subsystem, for accessing device-specific store features.
    m_StoreExtensionProvider = extensions;
    }
    public void OnInitializeFailed(InitializationFailureReason error)
    {
    // Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
    Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
    }




    I've checked it calls 'UnityPurchasing.Initialize(this, builder); ' normally.
    but after calling it, 'OnInitialized' and 'OnInitializeFailed', both are not called at all.
    Please help me.


    *When I try to buy an in-app item, it said it was not initialized.
    upload_2022-11-23_20-10-15.png