Sponsored By

Simplify the workflow of In-App Purchase in Unity3d

In this tutorial, you are going to learn the simplify workflow of using in-app-purchasing in Unity with IAP Manager. IAP Manager will take care all the logic of purchasing, buy button, price display and restore product. Minimal coding is needed.

Wai Lam Tsang, Blogger

November 22, 2016

6 Min Read
Game Developer logo in a gray background | Game Developer

There are many ways to monetize your mobile game. In-app purchase is the most popular one. But in Unity, it didn't have a straight forward steps to setup and manage IAP. You need a bit coding to intergrade into your project and hard code the product data into your code.

In this tutorial, you are going to learn the simplify workflow of using in-app-purchasing in Unity with IAP Manager. IAP Manager will take care all the logic of purchasing, buy button, price display and restore product. Minimal coding is needed.

Workflow of using IAP Manager

 

1. Setup Products in App Store

Follows the links below to setup products for in-app-purchase in each store. You will define the unique product id and product type for each product. For product such as coin or gold, you may setup as a consumable product. If your product is allowing to remove the advertisement, you may setup as a non-consumable product.

Tips: You can use same product id across store to simplify the workflow

iTune Connect (IOS)

Play Store (Android)

Amazon (Kindle)

 

 

 

2. Import IAPManager and Unity IAP

Setup Unity Purchasing Services

You can follow the tutorial below to setup Unity Purchasing Services (The scripting part is not requested).

https://docs.unity3d.com/Manual/UnityIAPSettingUp....

Download and import IAP Manager

Goto Window>Asset Store and search IAP Manager and import to your project. Or, Download IAP Manager in Asset Store directly.

 

3. Setup IAPManager in your scene

1. Drag and Drop the IAP Manager prefab into the scene. You can find the prefab from “Digicrafts/IAPManager/Prefab/IAPManager.prefab”.

 

2. Select the IAPManager object and open inspector. Set the number of products you have. Select the product type and fill the product ids.

 

 

 

3. Setup Buy Button in your scene

 

1. Create a UI Button in your scene by right click on the hierarchy window.

2. Connect the button to corresponding product.

3. (Optional) You can create a label to display the product title or price automatically.

4. (Optional) If your game have a non-consumable product. You can create a restore button and connect to the IAP Manager.

5. Play now. You should see the button fill with test data.

 

 

4. Connect the script object to IAPManager

1. Create an empty game object in your scene and add a new script with the following content. You can add the code to delivery your product here such as remove ads or reward coins.


using UnityEngine;
using System.Collections;
using System.Collections.Generic;

using Digicrafts.IAP;

 
public class IAPDCExample : MonoBehaviour, IIAPDelegate {




	//--- IIAPDelegate


 // Event when IAP initialized
	
public void OnIAPInitialized(Dictionary<string, IAPProduct> products){		
		// products contains a Dictionary whcih sotre product information
		foreach(KeyValuePair<string, IAPProduct> item in products){




			Debug.Log("Id: " + key + " Product: " + p);
    
            
		}
	}




	// Event when IAP initialized Fail
	public void OnIAPInitializeFailed(string error) {}




	// Event when a purchase started
	public void OnIAPPurchaseStart(IAPProduct product) {}




	// Event when when a purchase finished and success
	public void OnIAPProcessPurchase(IAPProduct product, string transactionID, string receipt) {




        // You can delivery your products here such as add coins, remove ads
		// You can get the product information and receipt from here.




	}




	// Event when a purchase failed
	public void OnIAPPurchaseFailed(IAPProduct product, string failureReason){}




	// Event for deferred purcahse
	// On Apple platforms we need to handle deferred purchases caused by Apple's Ask to Buy feature.
	// On non-Apple platforms this will have no effect; OnDeferred will never be called.
	public void OnIAPProcessDeferred(IAPProduct product) {}




	// Event for restore purchase
	// Success set to true if restore success
	public void OnIAPTransactionsRestored(bool success) {}
}

2. Drag the game object with the script to the Event Target of IAPManager.

3. Now, you can test your app for In-App Purchase.

 

Enabled Local Receipt Validation

IAP Manager also allows you to add client side receipt validation. Open player settings (Edit>Project Settings>Player). Add “RECEIPT_VALIDATION” in the Scripting Define Symbols. 

 

 

Then, you need to add the obfuscating encryption keys. Unity IAP provides a tool that can help you obfuscate your encryption keys within your Application. This confuses or jumbles the keys so that it is much harder for a user to acces them. In the Unity menu bar, go to Window > Unity IAP > IAP Receipt Validation Obfuscator. Place the Google Play Store Public Key here and press Obfuscate secrets.

Prepare for Amazon App Store

Amazon kindle use Android as platform. You need to package the apps with Android Apps.

Before build to Amazon, please select Window>Unity IAP>Android>Target Amazon. Then, start build your Android apk.

SaveSave

SaveSave

SaveSave

SaveSave

Read more about:

Blogs
Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like