Dynamically load Product Catalog from Online source
javierfed last edited by
I am attempting to build a dynamic product catalog, where products are querried from a website (database backend like MS Sql or JSON datafile). We will be updating the catalog on the mobile markets (IOS at this time, but plans to do google too) and want to just insert data into our online source and have our apps load this and build its internal system to display and be purchaseable.
primary example will be like books. each book will be individual, and we want users to be able to restore their purchases using their IOS app store reciept. we don't want to have to push an update each time a new book is added. how can we go about this using SIS? we have a license and everything, so would really like to get this concept working (at least a simple prototype for now.)
Baroni last edited by Baroni
thanks for the detailed explanation. Let's break down the mechanic of dynamically loading products into several key parts:
- Defining products that should be loaded, online
- Initializing Simple IAP System with those products
- Defining generic action on purchase
- Restoring owned content
Defining the product catalog can be done online - it is your choice where and how you would like to do it. For example, you could have a database with product identifiers like you wrote, or have a text file with an array of product identifiers that can be parsed after download. If you're using PlayFab already, you could also use its Title Data feature as written in this post:
After you've got the list of product identifiers, you can add them to Simple IAP System, so it initializes Unity IAP with these products. Since Simple IAP System typically initializes right after the scene loads automatically, you may have to delay its Awake() method by renaming and calling it manually, after your product catalog download finishes. At that point, add your products to the "IAPs" list of the IAPManager script. This is a list containing groups of items, as seen in the IAP Settings editor. Then, let it initialize.
As you know, the product action on purchase is defined in the IAPListener script, for each product identifier. Depending on the type of your content, you could let the callback start downloading an AssetBundle/image/encoded string or any other file from your server using a dynamic URL based on the product identifier.
For example in the IAPListener script:
(pseudo code) if productId starts with "book"
then download from https:\server.com\productId
That way and to keep it simple, your content file would have the same name as the product, so you can check it later on via DBManager.isPurchased and re-load it locally (e.g. from Resources).
- Restoring content should be handled already, since
- on Android, the purchaseSuccessful event -> IAPListener is called on the first app launch on a new install automatically, which then would start downloading the content again
- on iOS, the same process is applied but after manual player interaction entering his/her iTunes password
Make sure to not download existing content several times to avoid wasting bandwidth!
As there are several ways to handle dynamic products and content, I have tried to avoid listing code snippets in this explanation. Please look up specific code like downloading files from an URL on the official Unity scripting reference pages. If there is still anything unclear concept wise, please do not hesitate to follow up.