UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

architecture

[IAP] Expose Custom Store Extensions via UnityIAPServices

Solution

project architecturepackageseditor scripting

Unity 2021.x - Unity 6.3.x

Published Wed, Apr 29

Issue

 The challenge involves properly integrating a custom In-App Purchasing (IAP) store extension with Unity IAP 5. It is not immediately clear how to register such an extension or how other components, such as your store controller class or UnityIAPServices, should access its functionality. An explicit mechanism for exposing these custom store services is required to avoid breaking the IStoreService abstraction.

Custom IAP store extensions can be exposed by registering them with UnityIAPServices.AddNewExtendedStoreService. Access is then managed through UnityIAPServices.DefaultStore() or by specifying your store name with UnityIAPServices.Store().

Explanation

Custom IAP store extensions are registered and accessed through the UnityIAPServices API. Multiple store implementations can coexist within the system, allowing for flexible management of different purchasing environments.

  1. Register the custom store extension using the AddNewExtendedStoreService method on UnityIAPServices, providing a unique name and a factory method to instantiate your store extension class.
  2. Designate the custom store as the default for the application by calling SetStoreAsDefault("your store name") on UnityIAPServices. This reconfigures the DefaultStore() accessor.
  3. Retrieve the extension by its registered name using the Store("your store name") method if it is not set as the default.

Additional Tips

  • Ensure your store extension class inherits from ExtensibleStoreService to maintain compatibility with the UnityIAPServices framework.
  • Use RuntimeInitializeOnLoadMethod to register your extension automatically before the purchasing system initializes.

Copy


using UnityEngine;
using UnityEngine.Purchasing;
using UnityEngine.Purchasing.Extension;

public class CustomStoreExtension : ExtensibleStoreService
{
    public CustomStoreExtension(IStoreService baseStoreService) : base(baseStoreService) { }
    
    public bool PerformCustomAction() 
    {
        return true;
    }
}

public static class StoreRegistry
{
    [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
    static void InitializeCustomStore()
    {
        UnityIAPServices.AddNewExtendedStoreService("MyCustomStore", 
            (baseService) => new CustomStoreExtension(baseService));
        
        // Optional: Set as default provider
        UnityIAPServices.SetStoreAsDefault("MyCustomStore");
    }
}

Related Posts Haven't quite found a solution to your problem? We think these posts might help you.

Content inspired by a Unity discussion post.