Learning Huawei’s Unity Plugin 2.0 for your Unity Project

Dima Zangana
8 min readJun 30, 2021
Demonstrating Kits selection in Unity Editor

Want to learn how to use Huawei’s new Unity plugin that allows you to integrate your Unity mobile game or application to Huawei AppGallery? Then this is the right place!

This article is for the Unity Plugin 2.0 on version 2.0.8. As new updates come out, this article may eventually become outdated.

Topics:

  • Introduction to HMS & Unity Plugin 2.0
  • How to get the new HMS Unity Plugin
  • HMS Unity Plugin Kits & Features
  • Exploring the Kits
  • Want more help? Maybe use other kits?

Introduction to HMS & Unity Plugin 2.0

So, you’re probably wondering what HMS is? This is Huawei Mobile Services. This new Unity plugin is part of HMS.

Find more on HMS at: https://huaweimobileservices.com/

That’s right, this plugin is also 2.0, that’s because there is also another Unity plugin that was made before this one. It’s highly recommended to use the 2.0 plugin, and I’ll explain why.

The 2.0 version of the plugin has been created for the purpose of simplicity, to allow developers of all backgrounds such as junior developers to implement HMS services into their Unity projects. The plugin has been created in a way that reduces time significantly in the integration process in comparison to the previous version.

Features of 2.0

  • Reduced integration time and risk of integration issues.
  • Minimal lines of codes, almost codeless development experience.
  • Added a new custom Unity editor user interface for easy implementations. of kits that are aimed to help developers who are new to the process.
  • Optimised and has support for the latest HMS kit version by simply clicking ‘Check for Updates’ in Unity.
  • Gradle builds for a smooth procedure.
  • Removed library files inside Unity project.

These are the latest features in comparison to the previous version.

How to get the new HMS Unity Plugin

Currently, you can find the plugin on https://github.com/EvilMindDevs/hms-unity-plugin

  1. Head to the releases section and then download the package for your Unity version under Assets.
  2. In Unity, head to Assets > Import Package > Custom Package and select the .unitypackage file that was downloaded.
  3. Once prompted, click ‘import’ to complete the importing process.
  4. After importing the plugin, you will need to download the agconnect-services.json file from AppGallery Console and put it under Assets/StreamingAssets folder in the unity project.

If you are unsure how to do step 4, this probably means you haven’t yet set up a project on AGC (AppGallery Connect) — https://developer.huawei.com/consumer/en/doc/30204 — This link will help you set up your first app on AGC.

HMS Unity Plugin Kits & Features

Now that you have imported the plugin into your project, you’ll notice a new menu at the top that says ‘Huawei’. This button is important as this is where you can choose your kits and also check for the latest updates.

Exploring the Kits

Here, I’ll go through some of the kits that can be used in the plugin focusing on the game side of things;

1. Ads Kit

Planning to use Ads? It’s simple if you open the kits window again and simply mark the checkbox to enable the Ads kit.

Once you enable ads, you will then have another tab next to the selection of the kits called Ads, switch to this tab. You are now presented with 3 types of ads that you can use in your application or game. Banner ads, interstitial ads and rewarded ads.

Use Test Ads option: As you can see there is also a way to test your ads, this will however override any Ad ID’s you place in your fields, a useful way to check if the functionality is working correctly. So it’s probably better to actually first use test ads when doing this for your first time.

You can also create your own function to try and load all the ads that have already been enabled in the editor. Simply write; HMSAdsKitManager.Instance.LoadAllAds(); and this will load all the ads as intended.

Here are some examples of code you can write to call some of the ads:

This will allow you to show the banner ad and hide the banner ad when the function is called.

This one will allow you to show the interstitial ads as well as allowing you to hook OnInterstitialAdClosed function. This can be useful for your application for situations where maybe the user has finished the ad, and you want something to happen when it closes.

2. In-App Purchases Kit

If you haven’t already, make sure you head to the kit’s selection and check that you have activated In-App Purchases.

With this kit, there is a product list where you are able to register your products and then use them in your project.

Also, if you click the ‘Initialize’ on Start’ checkbox, this will allow the IAPs to be working when your app is launched.

If you are planning to do this manually rather than using the checkbox, you can use HMSIAPManager.Instance.CheckIapAvailability(); in your code when you want it to be initialized, such as after all your splash screens or when it loads the player’s data on your main menu, and so on.

Registering your IAP Products:

Writing your product identifiers and select the product type that you want to use. After you have done this, make sure you write the correct identifier, then click the Add button.

Initializing your IAP Kit:

If you’re planning to do this manually in code, here is an example below:

Once again, you can make some more hooks for when your IAP is purchased successfully or failed, feel free to apply that if you want it to follow your project’s logic and dependencies.

Setting up your IAP Constants Class:

There is now a useful tool that you can use that will make things more automated and this is by creating a constant class that will contain your registered products that you listed when you registered them. Below is an example of what it will look like when you create a class with registered products

Purchasing a Product:

Now with actually purchasing a product, you can use HMSIAPManager.Instance.BuyProduct();

Here is an example of how to purchase an IAP that you have registered and initialized.

3. Game Service Kit

This time, we are going to be enabling Game Service and using the available features such as Leaderboards and Achievements. So first, enable the Game Service kit as shown in the image above, this will automatically activate the Account Kit as well because it is dependent on the Account kit.

As previously mentioned before, you will then be able to use the Leaderboards and Achievements, so when you have enabled the kit, you will see another tab open where you are able to also add your types of leaderboards and achievements that you want in your application/game. You will also be able to create constant classes like the previous kits, making things more simplified and organised.

Now for the initializing part of the Game Service, you can simply click the ‘Initialize’ button and this when launching the kit as soon as the app is launched, or if you would like this to start in a specific part of your application, you can do this through code using: ‘HMSGameManager.Instance.Init’.

Showing your Leaderboards

So, you’ve managed to maybe create your leaderboards on AGC and also now in the HMS Settings in Unity?

At this point, as shown in the image above, these functions will allow you to actually show the leaderboard displaying the native UI when the function ShowLeaderboards() is called upon.

In the Start() function, once again using hooks to know when your leaderboard succeeded or failed to show. Make use of this as it can help with debugging down the line of development.

Reporting the User’s Score

Now you’ve got your leaderboard ready and want your user’s to actually submit scores to the leaderboard? It’s easy, as shown in the functions in the image above, you can submit it via the SubmitScore(); function. Just ensure that you pass the correct parameters for leaderboardId and score.

Showing your Achievements

Now, you’ve finished up with your leaderboards, it’s time to get those achievements working! So, this is as easy as the leaderboards also in regards to the code, simple as well. Calling the function ShowAchievements() will open up the achievements of your application in the native UI as well. You may also make use of the hook functions as well.

Unlocking Achievements

So you’ve managed to get your achievements to be displayed, it’s now time to unlock them! This process is also as simple as showing the achievements, calling the function: UnlockAchievement followed by the achievementId as the parameter is important is the way to unlock the achievement you want to unlock.

Also, feel free to use the listener events again to make your project more organised and reliable when it comes to debugging.

Want more help? Maybe use other kits?

If you feel like you didn’t manage to get everything you wanted to achieve and wanted to use some of the other kits, then good news! Don’t stop here at my article, there are several Huawei developers who have also posted useful articles that are for this new Unity plugin.

Feel free to check out all the links posted below:

Here’s a 3 part tutorial from Muhammed Enes Simsek:

Here’s from Samet Güzel:

Here’s from Yusuf Altun:

--

--