It’s a feature I would have needed for an app and seeing as there was a documentation for it, I thought, cool, let’s go. My thought was to fetch a JSON via a simple web call and then add the products.
But when I dug deeper and tried implementing it, I instantly stumbled on the lack of proper documentation?
The documentations in question:
There is a function called AddProduct() with several parameters but they don’t quite reflect what else can be added via the Catalog editor, namely localized descriptions, google configurations and apple configurations
I’d say setting the price for a product is pretty important, soooo… is it not possible to add products in runtime after all? Do I have to add products and update the app in the stores respectively every time a new item gets published? Or am I missing something in the documentation?
You can specify products in code at runtime, take a look at the configuration builder documented here
using UnityEngine;
using UnityEngine.Purchasing;
public class MyIAPManager {
public MyIAPManager () {
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct("100_gold_coins", ProductType.Consumable, new IDs
{
{"100_gold_coins_google", GooglePlay.Name},
{"100_gold_coins_mac", MacAppStore.Name}
});
// Initialize Unity IAP...
}
}
The price you allocated to the product, when submitting it to the store will be contained in the IAP response from the store, when a purchase is made at runtime.
Thanks for the very quick reply (even if mine now isn’t haha)
It sort of helps. I am now running into an initialization error though, saying “NoProductsAvailable”. I have two test products (“00_test” and “00_test2”) defined both on the google play console and the apple appstore, with the same IDs on the stores respectively.
The current process is
initialize the general UnityServices because it seems to be necessary
create an instance of the IAPManager
which also adds the products (product IDs fetched via JSON),
Ok quick update- IAP initialization works now but NoProductsAvailable for Purchase Initialization this time, claiming the catalog does not have the test products. It is after enabling the IAPButton Component after instantiation. The error is in the editor, I have yet to test on the devices.
In your logs, do you see any messages starting with “Unavailable product”?
If you do, can you post the log message and screenshots of your products in the stores?
If you don’t, this means you are trying to initialize IAP with no products at all which will cause the initialization to fail.
If you are fetching your products via an external JSON and then calling the builder.AddProduct, the call might be happening too late while IAP is already initializing.
My suggestion would be to go with coded IAP and not use the Catalog (make sure it’s empty or the “Automatically initialize UnityPurchasing” is off) where you can control when to call UnityPurchasing.Initialize to make sure your products are added before trying to initialize.
If you are unsure how to implemented coded IAP, I recommend taking a look at our sample project: