So, you have this shiny game you made in Unity3D. You have tested your monetization funnel. You have created all of your In-App Purchase items (IAP). All you need now is to integrate it with Amazon Appstore In-App-Purchasing APIs. There 2 ways to integrate the Amazon IAP API into your Unity game. You can use the built-in cross-platform Purchasing API from Unity, or you can use our own Unity Plugin. In this blog, we will look at a basic comparison of the two methods, and the advantages and disadvantages of each. We will also walk through setting up IAP items and implementing the Unity Purchasing API in your game.
In our next installment, we will walk through the same process with the Amazon IAP Unity plug-in.
Unity Purchasing API
The Unity purchasing API is an abstraction layer built on top of the Amazon Unity Plugin, as well as other store plugins.
- You can switch between different stores with 1 code base, including:
- Amazon Appstore
- Google Play
- Samsung Store
- Apple Appstore
- Windows Store
- Maybe more in the future.
- It’s super easy to implement.
- You can use the feature of managing your IAP items on the Unity Cloud Analytics.
- You won’t always get the latest API features from the stores.
- You have to upgrade Unity as a whole when bug fixes are implemented to the API.
Amazon Unity Plugin
This is the plugin that Amazon maintains and updates frequently with new features and bug fixes.
- You get the latest and the cutting edge of what Amazon Appstore provides.
- You have the flexibility to use the API as you want, not what the engine wants.
- The setup process is more complex.
- Having a different codebase for every store can get complicated for your game.
- You won’t have access to the IAP Unity Cloud management feature.
Unity Purchasing API walk through
For this post, we will show you a demo on how to get started with your first purchasable item using the Unity Purchasing API. Let’s start by adding the Unity Purchasing API to your game. There is a good guide from Unity on how to get started here. In summary, it says:
- Link your project with your Unity Services Account from the services tab of Unity.
- Enable IAP in the services tab of Unity.
- Do your COPPA compliance tasks. If you are targeting kids under the age of 13, there are some legal questions you have to answer.
- Adding the IAP package to Unity.
See, that was easy, right? Now let’s work on integrating the API with your game. First, start with creating a new C# behavior in unity called Purchaser. This is the class that is going to manage your purchasing actions. You can name it whatever you want; it’s your class. For this walkthrough, we are going to call it Purchaser.
// Placing the Purchaser class in the CompleteProject namespace allows it to interact with ScoreManager,
// one of the existing Survival Shooter scripts.
// Deriving the Purchaser class from IStoreListener enables it to receive messages from Unity Purchasing.
public class Purchaser : MonoBehaviour, IStoreListener
// The Unity Purchasing system.
private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems. This gets set automatically based on the settings you set in the editor. Or you can force set it programically by the following line
Now, if you have not already done so, let’s go to the Amazon Developer Console and create a new entry for your game. Once you are in the game page, click on In-App Items.
Here you can create different types of items:
- Consumables. These are items that players can purchase over and over again. Like currency, or extra lives.
- Entitlement. These are items that a player can only purchase once, like unlocking levels, or a super special item they can’t throw away.
- Subscriptions. These are recurring payments (based on a period you choose) that let the player subscribe to content, data, regular refills of consumable items, or anything you can imagine!
Now add all the items you have there, along with unique SKUs that you can remember; we will use them in our game soon. For example, let’s assume you added a consumable item with the SKU “100_gold_stars".
We have already created a class with the Unity Purchasing system enabled, and we also have created the store specific subsystem set. We just finished setting up the items in your developer console, and now it’s time to add the IAP items. Here, we will be adding 1 consumable item to the configurationBuilder. It’s a class that can be used to add IAP items to your game and adding different configurations for each store. This consumable item would give 100 gold stars to the player.
// the Configuration builder will help us add the IAP items for each store, for more info about it check the unity documentation.
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
//this will use the builder we just created and initialize the purchasing API with the configuration.
Now we have a purchasing class ready to do some purchasing. Let’s add these skeleton functions:
/// Called when Unity IAP is ready to make purchases.
public void OnInitialized (IStoreController controller, IExtensionProvider extensions)
this.controller = controller;
this.extensions = extensions;
/// Called when Unity IAP encounters an unrecoverable initialization error.
/// Note that this will not be called if Internet is unavailable; Unity IAP
/// will attempt initialization until it becomes available.
public void OnInitializeFailed (InitializationFailureReason error)
/// Called when a purchase completes.
/// May be called at any time after OnInitialized().
public PurchaseProcessingResult ProcessPurchase (PurchaseEventArgs e)
/// Called when a purchase fails.
public void OnPurchaseFailed (Product i, PurchaseFailureReason p)
These functions will be called back based on what happens when you initiate a purchase. So to initialize a purchase, make sure you have an instance of your Purchasing class, then call the InitiatePurchase() function passing the SKU as a string.
var purchasing = new Purchasing();
You can learn more about how to use the Unity Purchasing API from Unity’s own documentation. Next we will do the same exercise using Amazon’s own Unity plugin.