Using Account Kit, Ads Kit and Analytics Kit with a Simple 2D Unity Game using HMS Unity Plugin

Sunandhini Muralidharan
Huawei Developers
Published in
7 min readJun 23, 2023

The HMS Unity plugin helps you integrate the full power of Huawei Mobile Services into your game developed with Unity.

HMS Unity Plugin

Introduction

Unity is a cross platform Game Engine developed by Unity Technologies. Huawei Mobile Services is a very innovative, complete Mobile Ecosystem from Huawei. HMS Unity plugin helps developers to use HMS in Unity Game.

This article focuses on using HMS Unity Plugin and integrate few basic kits like Account Kit, Ads Kit and Analytics Kit into a simple Unity 2D Game developed by me.

HMS Unity Plugin — Overview

HMS Unity Plugin is used to integrate HMS capabilities into the games in Unity without worrying about boilerplate code. All the back-end code is already dealt in the plugin and all the developers have to do is focus on game logic and HMS feature needed for the game.

Before we begin, please visit the Official Release page of the plugin. The README has general information about the plugin. You can also go through the documentation of the plugin.

My Game

My game is a classic Snake Game in 2D. It has all the elements and features of the classic snake game like the wrap around, death, eating the food, score, etc.

Kindly note that we will not concentrate completely on coding the game logic. Rather our focus is on integrating the plugin with the game.

Snake Game with Banner Ad

This game consists of one Game Scene. It has scripts for handling the game, Game Assets, Snake, Food Grid and score. We will be using these scripts to integrate the HMS Unity Plugin. I will be integrating Account Kit, Ads Kit and Analytics kit in this article. I will be integrating Push kit, Crash kit, Game Service and IAP in another article.

Pre-Requisites

Hardware Requirements

  • A Huawei Phone/Tablet running EMUI 3.0 or later
  • A Non-Huawei Phone Running Android 4.4 or Later

Software Requirements

  • Unity Editor: 2018, 2019, 2020 or 2021
  • minSdkVersion: 21 or later
  • Gradle: 5.4.1 or later
  • HMS Core (APK) version: latest version recommended To view the HMS Core (APK) version on a device, go to Settings > Apps > Apps and search for HMS Core.

Note:

Non-Huawei phones do not have HMS Core pre-installed. It has to be installed.

Gradle 5.4.1 or later already exists in Unity 2020, 2021 and 2022 Versions. But, Unity 2018 and 2019 versions require Gradle Configurations

I have developed this game in Unity 2020.3.48f1 (64-bit). For more latest versions, you should be able to do the same steps. For 2018 and 2019 version, please check the Plugin release page for the updates. I further suggest you to always use the latest version of the plugin.

Development Process

1. Project Preparations

There are certain preparatory steps that need to be done to start using the plugin. Also, the game developer must have decided what kits needed to be used for his game and how to use them.

  • Create Unity Project
  • Register your app at AppGallery Connect and Configure the Signing Certificate Fingerprint. Refer here for more information.
  • Download the Unity Plugin (.unitypackage file) from here.
  • Import the plugin into your Unity Project. Refer here for more information.
  • Select the required kits from Manage API section in AGC and download the agconnect-services.json.
  • Copy/Replace the agconnect-services.json into Assets -> Streaming Assets folder of your Unity Project.
  • Go to File -> Build Settings (Android) -> Player Settings -> Other Settings. Enter the package name that you gave while creating the App in AGC (The package name is also available in agconnect-services.json). Also, make the Minimum API Level to 21.
  • Connect with your kits by selecting the kits required for the game from Huawei -> Kit Settings Option. Refer here for more information.

Note: Refer the below link for detailed step-by-step procedure for integration.

2. Integrating the Kits

Once you have enabled the kits in the kit settings, kit manager will be enabled and these managers have DontDestroyOnLoad implemented and hence reference is not lost. Visible Kit managers are not seen as Game Object after the plugin V2.2.

In my game, GameHandler.cs is the script which acts as the entry point and controls the game. Snake.cs script will do the functionalities required for the snake behavioud. LevelGrid.cs script helps in spawing the food at random grid positions and GameAssets.cs holds all the Assets required for the game. ScoreCalculator.cs will calculate and reset score

Account Kit

Signing into Huawei account will happen at the start of the game and signout will happen when the snake dies. HMSAccountKitManager will be enabled when kit is enabled. Use HMSAccountKitManager to access account kit methods. The following is the way to access these methods.

HMSAccountKitManager.Instance.SignIn();

HMSAccountKitManager.Instance.SignOut();

Sign In is done in GameHandler.cs when the game starts while Sign Out is done in Snake.cs when the snake dies.

If you are going to use Game Service, it has automatic Sign-In functionality. so, instead of using Account Kit, you can just enable Game Service and tick “Initialize on Start” and it will automatically sign in every time the app is opened. We will see more of this in next article.

Ads Kit (Banner Ads and Splash Ads)

Enable the Ads kit in the Kit Settings and configure the Ads slot ID in the Ads Tab as shown below. I have used Splash Ads and Banner Ads for now. Splash Ads are full page ads that will be shown once when the game starts while Banner ads are rectangular images shown either at the top, middle or bottom within the app layout. Regular functionality of the app can proceed when banner ads are shown.

SnakeUnity

You can enable the test ads to test your application. You can use actual Ad Ids when you are ready for release. Click on preview to see the preview of the Splash Ads.

Interstitial ads are full-screen advertisements that appear during natural breaks or transitions within an app or game. Rewarded ads are a type of advertisement that offers users in-app rewards, such as virtual currency, extra lives, or premium content, in exchange for watching a short video or engaging with the ad. We will enable rewarded ad when we talk about IAP in the next article. Refer to the below code for showing and hiding the ads.

HMSAdsKitManager.Instance.ShowBannerAd();
HMSAdsKitManager.Instance.HideBannerAd();

HMSAdsKitManager.Instance.ShowInterstitialAd();
HMSAdsKitManager.Instance.OnInterstitialAdClosed = OnInterstitialAdClosed;

HMSAdsKitManager.Instance.ShowRewardedAd();

HMSAdsKitManager.Instance.OnSplashAdClicked += OnSplashAdClicked;

Analytics Kit

Analytics Kit is a one-stop user behavior analysis platform for products such as mobile apps, web apps, quick apps, quick games, and mini-programs. It offers scenario-specific data collection, management, analysis, and usage, helping enterprises achieve effective user acquisition, product optimization, precise operations, and business growth.

After enabling Analytics Kit from Kit Settings, you can use HMSAnalyticsManager to log some specific event for analysis using SendEventWithBundle function. There are automatic events that will be triggered without any intervention from the developer. But, we can send custom events when some events happen in the game. In this game I have logged an event whenever Sign In function is called and whenever Snake Eats the food. We will be adding more events when we integrate IAP in next follow-up article.

3. Code

HMSAccountKitManager.Instance.OnSignInSuccess is a callback which will say what to do when the sign in operation is success. Likewise we can determine callbacks for different actions like if sign in if failed, if user clicked the ads, etc.

Our integration is over. Lets now see the output.

Output

Let’s game
App Debugging

You can enable the App Debugging in command mode to see the events in the App Debugging section of AGC. Please type the below command to enable the App Debugging.

adb shell setprop debug.huawei.hms.analytics.app package_name_from_agconnect

Tips and Tricks

  • Do not forget to add the agconnect-services.json in Assets -> StreamingAssets folder of Unity Project.
  • Dont forget to import the libraries for the plugin. For Account Kit, Ads Kit and Analytics kit, I have used the following imports.
using HmsPlugin;
using HuaweiMobileServices.Ads;
using HuaweiMobileServices.Id;
using HuaweiMobileServices.Utils;
using HuaweiMobileServices.Analystics;
  • While creating app in AGC, please make sure that the package name ends with .huawei or .HUAWEI for your game to work properly. If this is not included, certain components of the plugin might not work.
  • To see all the Functions in each kit, go to Assets -> Huawei -> Scripts in the Unity Project where there will be a folder for each Kit manager.

Conclusion

This article gives a detailed insights into how Account kit, Analytics kit and Ads kit of HMS Unity Plugin can be integrated and how each kit is useful for the game/app. The speed of integration is very quick and we can integrate any kit and see the result perhaps very fast depending on the complexity of the Game.

I hope this article is helpful. Do try this and let me know your feedback in the comments section. Also, if there are any unanswered or untouched points, kindly feel free to add in comments as well.

Game Service and IAP will be covered in next article.

References

--

--

Sunandhini Muralidharan
Huawei Developers

HMS, Android Studio, Java, Flutter, Dart Programming, HMOS