How do I integrate HMS to my game using HMS Unity Plugin 2.0? — Part 3

Muhammed Enes Simsek
Huawei Developers
Published in
8 min readApr 16, 2021
HMS Unity Plugin 2.0

Introduction

Before I begin, if you are not coming to this article from the Part 2 of the series, you can read here. If you have not seen part 1 either, you can click here. This is the third part of our HMS Unity Plugin 2.0 integration guide. This time I will be talking about in-app purchases, i.e. IAP. In the part 2 of the series, I already integrated many kits to my simple game but one important element has been missing: IAP. I will integrate IAP so that users can spend money on items in my app and you can make revenue out of your game.

I will, again, try to keep it brief and use a simple scenario. I will be implementing two types of products. One of which is a consumable and the other is a non-consumable. As you might now, in IAP, there are three main categories, two of which I already have given the names of and the third is subscription. All of them are self explanatory; for example, a consumable is a coin that can be spent and re-bought, a non-consumable is a premium membership that can be bought once and will not be lost, and a subscription is a subscription to a music or video service.

Since my game is simple, to try out more than one category, I use “heart” as a consumable purchase and “remove ads” as a non-consumable purchase. That is, since my game has hearts on the left upper corner to track how many lives of the player is left, I add an in-app purchase to buy one heart. Also, since I use ads, I will use a remove ads purchase option and if the user buys it, I will remove the ads from the game.

Let’s get started!

IAP

Before going into coding phase, there are important things that you should do.

Sandbox Testing

After I am done with the whole IAP process, I will have to test whether I implemented everything right. However, since this is IAP, I would need a credit card and purchase every time in real money, even for test purposes. This is not optimum because then, I would need funds and the refunding process takes a bit long for testing.

Huawei offers “Sandbox Testing” for this type of scenario. You need to configure a sandbox testing account for the account that you use to publish your game in App Gallery. For all the details, please click here. This is not essential for IAP to work but it is a very useful feature for testing. I strongly suggest you configure this before testing. I will not talk about the details here but the link should have it all.

Merchant Account and Other Settings

IAP requires a merchant account in the App Gallery side. For the general walkthrough in official docs please check here. For the merchant account, you can check here. Naturally, for the money you earn to be transfered to your account, you would need to provide your bank details to App Gallery. Links have the details and from now on, I will assume you have completed these steps, so we can code together in peace.

Coding Phase

Let’s get to coding in our app. You need to determine your specific scenario where you want to implement IAP and which kind of products you will offer to your users. I already talked about my scenario, so let me show you how I configured my app to implement that process.

Implementing a Pause Menu

I added a pause menu to my game so that whenever users want to buy additional hearts to survive longer or to remove the a-bit-annoying ads, they can do so by pausing the game. I added button to that menu and implemented button clicks to direct them to App Gallery UI for IAP and then to grant them what they just bought.

My pause menu looks like this. I implemented direct button clicks

Now that my pause menu is ready, I need button clicks. But before that, just like achievements in part 2, I need to add my products to the App Gallery Connect. Plugin will help me get those products easily thanks to its IAP tab in HMS Settings menu. So, before adding some button clicks let’s go to AGC and add our products.

Adding Products to AGC

What you first should do is to go to AGC console by clicking here, sign in and then go to My Apps and then choose your game. You will be directed to “Distribute” tab. From the left-upper bar, choose “Operate” tab instead. There, you will have “Product Management” tab opened at first, which is where you will add your products.

AGC screenshot of my game with two products.

Click add product on the right-upper corner and enter the details of the product that you think is suitable for your own game. Please also consider the scenario where and when you will allow your users to buy this product because it can slightly change your product details.

Add a product menu

My remove_ads example can be seen above. I very simply provided a name and explanation. The most important thing here is the product ID because I will reach our product from the game using that ID.

Adding Products to Plugin

After I add both of my products, now it is time to set up the plugin constant class. For my case it may look redundant because the IDs of my game is short and there are only two of them anyway, so they are easy to remember. However, plugin is designed to help all kinds of developers, also to those who may have over a thousand IAP products in their apps. It basically provides ease of use.

So, I open the HMS Settings from the Huawei tab in Unity editor.

HMS Unity Plugin 2.0 IAP Settings Screen

You simply should select the type of the product and enter its ID. ID must match the ID that you used in AGC. You can also import all your products, if you already have a game on Google Play Store by downloading the report and importing it to HMS Unity Plugin 2.0.

“Initialize on Start” will call certain functions at the beginning of your app and make the IAP ready for you. If you tick the box, you do not have to worry about the IAP initializations that are normally required, plugin will do it for you.

In a scenario where you do not want this to be your default behaviour (for instance, in cases of huge load on Start() function etc.), you can leave it unchecked because it is also possible to manually initialize IAP service. For this please refer here, the official readme of the plugin. There are just a few things that you should do. If you do them, you should be good to go and use the IAP as usual.

And that’s it for the HMS Settings part. Now to code in C#.

Implement Button Clicks and Callbacks

What is left to do is to implement the button clicks for purchasing the products. But also, implementing a success callback is a must because that callback is where I will implement my post-purchase logic. It may differ from one app to another, so I will show you my logic and you can infer what to for your own case.

In a separate script, or in a script that you think is suitable, I need to first register to callback. Since I have the plugin on my side, we do not have to deal with anything else, especially if you ticked the “Initialize On Start” box. The products will be retrieved from AGC in the backend, so what you should just do is to call purchasing functions.

void Start()
{
//...
HMSIAPManager.Instance.OnBuyProductSuccess = OnBuyProductSuccess;
}
public void BuyProduct(string id)
{
HMSIAPManager.Instance.BuyProduct(id);
}

In the code above, I registered to success callback and made a function call my actual function to buy the product. I did it this way to implement a button click but normally one line code as shown below is enough:

HMSIAPManager.Instance.BuyProduct(HMSIAPConstants.remove_ads);

I also should add the callback, where I will implement my post-purchase logic.

[HideInInspector]
public bool isAdsRemoved = false;
private void OnBuyProductSuccess(PurchaseResultInfo obj)
{
string myProductId = obj.InAppPurchaseData.ProductId;
//Implement your own logic here...

if(myProductId.Equals("heart"))
{
GameObject.Find("Player").GetComponent<Player>().health++;
GameObject.Find("Player").GetComponent<Player>().updateHealthDisplay();
}
else if (myProductId.Equals("remove_ads"))
{
isAdsRemoved = true;
}
}

In the success callback, assuming that I have no server side implementations, I will get my product id to see which product the user has purchased. The rest is completely up to you because it is where you implement your own logic.

What I did is, if the user has purchased a heart, I increment his health by one and update the health display on canvas.

If the purchased product is remove ads, I set my boolean variable to true. I use this wherever I show ads to user in an if check, so if the user has purchased the “remove_ads” product, s/he will not see the ads in my game.

Tips & Tricks

  • Normally, a non-consumable product, once purchased, cannot be purchased again. However, in Huawei Sandbox Testing, you are able to purchase the non-consumable (remove ads in my case) product again.
  • If you are unsure about whether you successfully registered in the sandbox testing environment, you can try purchasing a product in-game. You will be directed to IAP UI and before you can try to buy anything, you should see a warning message, like below, if you are in sandbox testing. If you do not see anything and the app wants a credit card, then, your sandbox testing process is failed and you are advised the visit the steps again.
Sandbox test alert dialog

Conclusion

That’s it we successfully integrated IAP to our game together! The users will be able to do in-app purchases in the game and we can even start making money!

If you have completed other two parts of this article series too, your game is now empowered with Huawei’s powerful kits and that is with the ease of low development cost, thanks to HMS Unity Plugin 2.0.

I hope that this article series have been helpful for you. You can always ask questions below, if you have anything unanswered in your mind.

Good luck on the store and see you in my other articles!

References

--

--