[IAP] Expose Custom Store Extensions via UnityIAPServices
Solution
Unity 2021.x - Unity 6.3.x
Published Wed, Apr 29
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().
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.
- Register the custom store extension using the
AddNewExtendedStoreServicemethod on UnityIAPServices, providing a unique name and a factory method to instantiate your store extension class. - Designate the custom store as the default for the application by calling
SetStoreAsDefault("your store name")on UnityIAPServices. This reconfigures theDefaultStore()accessor. - 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
ExtensibleStoreServiceto maintain compatibility with the UnityIAPServices framework. - Use
RuntimeInitializeOnLoadMethodto register your extension automatically before the purchasing system initializes.
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.