In-app Purchases Unity tutorial

Implement safe IAPs in your Unity Project in less than 1h time in three steps:

  1. Create the product
  2. Set up the project
  3. Prepare purchase process

Create a product in the stores

This is the most important part of this tutorial. Without a product to be purchased, what are you going to buy?

So, let’s create a new product!


1. Android: Google Play Console

  • Create a Managed product in the Google Play Console called “New Map Unlocked” with productid “newmap”.

  • Before being allowed to create products in the Google Play Console for you app, you'll need to upload an APK file (Alpha, Beta or Production) with the BILLING permission enabled.

Each store has it’s own tricks and setup process, you should create every product independently.

For Android, you have to completely setup your app, because you need the App to be published in order to test it with real products.

Remember that you can upload an Alpha or Beta APK that only will be available to the white listed users.

Do so following this guide.


2. iOS: iTunes Connect

  • Create a Consumable product without errors in iTunes Connect called “New Map Unlocked” with productid “newmap”.

     

  • Remember to select the available products for the current version of the app.

  • If you did everything correctly, your app should be ready to upload binary. If you have any other errors when trying to publish, fix them to avoid a failure in the in-app purchase process.

Each store has it’s own tricks and setup process, you should create every product independently.

For iOS to setup an app enabled for in-app purchases, please follow this guide.


Products created!

Congrats! You've finished setting up your products for both app stores. Now you can move on!

 

Setup the Unity project


1. Add the GamedoniaStoreInAppPurchases script (located under Assets/Plugins/Gamedonia ) to your Gamedonia game object.

You may have other scripts attached to the Gamedonia GameObject, it's ok if you add them for other Gamedonia solutions.

2. If you are developing for Android, set the Android Publickey. It's the Public key assigned when setting up Google In-App Purchases. Visit the Services & APIs section in the Google Play Console.

3. If you are developing for iOS, leave the Android Publickey blank.

4. Setup the AndroidManifest.xml File located at Assets/Plugins/Android (Android developers only).

a) Uncomment the Billing permission that comes with the Gamedonia Unity Package:

<uses-permission android:name="com.android.vending.BILLING"></uses-permission>

b) Uncomment the Gamedonia BillingActivity and service that comes with the Gamedonia Unity Package:

<activity android:name="com.gamedonia.inapppurchase.BillingProxyActivity" android:theme="@style/Theme.Transparent"></activity>
<service android:name="com.gamedonia.inapppurchase.UnityAndroidInterface"></service>

Purchases set up in Unity!

You're almost done. Now you've set up purchases for your Unity project, you can move to the last step of this tutorial. Let's go!

 

Prepare the purchase process

Before purchasing a product, your game has to request the product list and details to the store.

This step is mandatory in order to show the user the localized price and name. After requesting the product list you can purchase a single product.


1. Request the products:

The simplest and easiest place to make the request would be on your home scene, after the user logs in. You can make the request product call whenever you want, but it must always be before the first purchase.

a) Add this code to the Start() Method of your home scene script:

//Register the callback
GDInAppService reqService = new GDInAppService();
reqService.RegisterEvent += new InAppEventHandler(OnProductsRequested);
GamedoniaStoreInAppPurchases.AddRequestService(reqService);

b) Add this new function to the home scene script:

private void OnProductsRequested() {

	if (GamedoniaStore.productsRequestResponse.success) {

		foreach (KeyValuePair<string, Product> entry in GamedoniaStore.productsRequestResponse.products) {

			Product product = (Product)entry.Value;
			Debug.Log("Received Product: " + product.identifier + " price: " + product.priceLocale + " description: " + product.description);
		}
	} else {
	
		Debug.Log ("Unable to request products!, message: " + GamedoniaStore.productsRequestResponse.message);
	}
}

c) Request the products on the method OnFacebookLogin we made in the Data Storage tutorial, when the login is successful:

string [] productsList = new string[]{"newmap"};
GamedoniaStore.RequestProducts(productsList, productsList.Length);

With that code, when the user makes a login we will call the store to retrieve the list of products in their localized version. It's a good idea to keep them in a Dictionary to use them afterwards in the in-game shop or other places.

2. Make the purchase:

Similar to the request products process, you need to register a callback method to be notified when a product has been purchased. Choose the scene where the product is going to be purchased, usually the shop, but you can also make a Buy button to test this tutorial.

a) In the Start() method of your scene add the following code:

GDInAppService buyService = new GDInAppService();
buyService.RegisterEvent += new InAppEventHandler(OnProductPurchased);
GamedoniaStoreInAppPurchases.AddPurchaseService(buyService);

b) Somewhere in the same scene add the following new method:

private void OnProductPurchased() {

	PurchaseResponse purchase = GamedoniaStore.purchaseResponse;
	string details = "Purchase Result status: " + purchase.status + " for product identifier: " + purchase.identifier;
	if (purchase.message != null && purchase.message.Length > 0) details += " message: " + purchase.message;
	
	Debug.Log(details);

	if (purchase.success) {

		Debug.Log("New Map Unlocked");
		// UNLOCK YOUR NEW MAP
	}
}

c. Make the purchase on the click method of your button:

if (GUI.Button(new Rect (80,205, 220, 50), "BUY NOW!")) {

	GamedoniaStore.BuyProduct("newmap");
}

Full purchase setup completed!

Tutorial completed! Your game is now ready to make purchases both in Google Play and in the App Store. Good job!

What's next?

The following step will show you how to add Downloadable Content (DLC) to the project using Gamedonia. Distributing new content anywhere, anytime will be super easy after following the tutorial.

If you want check the DLC tutorial, check it out here .

This is the complete list of tutorials to help you build with Gamedonia Backend:

  1. Facebook login
  2. Data storage
  3. Server Code
  4. Push notifications
  5. In-app purchases
  6. Downloadable content