Use Unity In-App Purchasing (IAP) APIs to initialize purchasing, map Microsoft Partner Center Store IDs to products that you want to sell, and handle purchases in your game.
Before you implement the purchase workflow, complete Set up Microsoft Partner Center and Unity Editor to install required packages and configure your products.
Important: Initialize the XGameRuntime before making any IAP calls. Unity IAPAbbreviation of Unity In App Purchase
See in Glossary requires this for Xbox support.
Follow these steps to implement purchases:
For more information, refer to the Unity IAP documentation on Initialize In-App Purchasing.
For more information, refer to the Unity IAP documentation on Define products programmatically.
Note: Each product in Xbox Store has its own Store ID generated by Microsoft Partner Center when you create add-ons. Enter this ID into the ProductDefinition as the storeSpecificId.
If your game targets only Xbox Store, construct your ProductDefinition with the product’s Store ID as the storeSpecificId:
ICatalogProvider catalogProvider = new CatalogProvider();
var products = new List<ProductDefinition>
{
new("com.mygame.coins100", "9PEXAMPLE001", ProductType.Consumable),
new("com.mygame.extralevel", "9NEXAMPLE002", ProductType.NonConsumable)
};
catalogProvider.AddProducts(products);
If your game supports multiple stores and you want the same product on these stores, use the StoreSpecificIds mapping:
ICatalogProvider catalogProvider = new CatalogProvider();
var products = new List<ProductDefinition>
{
new("com.mygame.coins100", ProductType.Consumable),
new("com.mygame.extralevel", ProductType.NonConsumable)
};
var coinIds = new StoreSpecificIds
{
{ "9PEXAMPLE001", XboxStore.Name }
};
var extraLevelIds = new StoreSpecificIds
{
{ "9NEXAMPLE002", XboxStore.Name }
};
var storeSpecificIds = new Dictionary<string, StoreSpecificIds>
{
{ "com.mygame.coins100", coinIds },
{ "com.mygame.extralevel", extraLevelIds }
};
catalogProvider.AddProducts(products, storeSpecificIds);
For more information, refer to the IAP documentation on Fetch products.
Note: It’s recommended to fetch products using ICatalogProvider.FetchProducts, because the StoreSpecificIds are already mapped when you define products. If you want to fetch products using StoreController.FetchProducts, create different ProductDefinition lists per store. The following code example demonstrates how to create product lists based on the store:
var products = new List<ProductDefinition>();
var storeName = DefaultStoreHelper.GetDefaultStoreName();
if (storeName == XboxStore.Name)
{
products.Add(new ProductDefinition("com.mygame.coins100", "9PEXAMPLE001", ProductType.Consumable));
products.Add(new ProductDefinition("com.mygame.extralevel", "9NEXAMPLE002", ProductType.NonConsumable));
}
else
{
products.Add(new ProductDefinition("com.mygame.coins100", ProductType.Consumable));
products.Add(new ProductDefinition("com.mygame.extralevel", ProductType.NonConsumable));
}
var storeController = UnityIAPServices.StoreController();
storeController.FetchProducts(products);
For more information, refer to the Unity IAP documentation on Fetch purchases.
For more information, refer to the IAP documentation on Purchases.
Use entitlement checks to confirm that the user has valid access to the purchased product. For more information, refer to the Unity IAP API documentation on CheckEntitlement(Product).
After you complete the implementation, verify your purchase workflow in a sandbox environment.
Unity IAP for Xbox Store doesn’t support the following features: