📜 ⬆️ ⬇️

IAP in Unity3D

Once upon a time, during the days of Unity 4, the addition of in-game purchases caused some difficulties. You could go in two ways: use any plugin from the existing ones or implement your wrapper over the native functions for each platform. In the first case, there were several solutions: Soomla, OpenIAB, Prime, and many, many others. Some of them were paid and were quite expensive: the price of Prime was about $ 70. Some were free and refused to work in iOS: OpenIAB.

Unity3D 5.3


And in the version of Unity3D 5.3 appeared in-app support for purchases as they say out of the box. This simple tool makes it easy to embed in-app purchases for the most popular app stores.

Now supported:


Project Integration


To begin, in the Services tab, click on the In-App Purchasing field and enable this feature. The analytics service is also automatically turned on, in which you can later see the Revenue, Average Revenue Per Paying User (ARPPU), Average Revenue Per Daily Active User (ARPDAU).
')


Next, you need to click on the Import button and then, all the necessary assets will be imported into the project.


In-app manager


Now you need to add code to work with IAP. This C # script contains the following functions:


Use in the game


To make it clearer how to use the script, I will give an example. There are two purchases: one NonConsumable - disabling advertising, the second Consumable - gives the player 80 coins. Two platforms are used: Google Play, AppStore. For each purchase, you must declare three constants that contain the identifiers of purchases UnityIAP, Google Play and AppStore.

public const string pMoney80 = "money_80"; public const string pNoAds = "no_ads"; public const string pMoney80AppStore = "app_money_80"; public const string pNoAdsAppStore = "app_no_ads"; public const string pMoney80GooglePlay = "gp_money_80"; public const string pNoAdsGooglePlay = "gp_no_ads"; 

After that, these constants must be passed to the initialization function:

 builder.AddProduct(pMoney80, ProductType.Consumable, new IDs() { { pMoney80AppStore, AppleAppStore.Name }, { pMoney80GooglePlay, GooglePlay.Name } }); builder.AddProduct(pNoAds, ProductType.NonConsumable, new IDs() { { pNoAdsAppStore, AppleAppStore.Name }, { pNoAdsGooglePlay, GooglePlay.Name } }); 

Now on the purchase button of the item, you need to hang the BuyProductID function with passing it the identifier of the item being sold as a parameter.



And in the PurchaseProcessingResult function, add actions for each of the purchases.

 if (String.Equals(args.purchasedProduct.definition.id, pMoney80, StringComparison.Ordinal)) { //Action for money ResourceManager.Instance.Money += 80; } else if (String.Equals(args.purchasedProduct.definition.id, pNoAds, StringComparison.Ordinal)) { //Action for no ads ResourceManager.Instance.NoAds = true; } 

That's all. It remains only to remember to create purchases with the same identifiers in the AppStore and Google Play.

Source: https://habr.com/ru/post/320224/


All Articles