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.
The Unity purchasing API is an abstraction layer built on top of the Amazon Unity Plugin, as well as other store plugins.
This is the plugin that Amazon maintains and updates frequently with new features and bug fixes.
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:
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.
using System; using System.Collections.Generic; using UnityEngine; using UnityEngine.Purchasing; using UnityEngine.UI; // 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 UnityPurchasingEditor.TargetAndroidStore(AndroidStore.AmazonAppStore) }
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:
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.
Public Purchaser(){ // 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()); builder.AddProduct("100_gold_stars", ProductType.Consumable); //this will use the builder we just created and initialize the purchasing API with the configuration. UnityPurchasing.Initialize(this,builder); }
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) { return PurchaseProcessingResult.Complete; } /// /// 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(); purchasing.InitiatePurchase(“100_gold_stars”);
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.
Stay tuned!