How to use Huawei Mobile Services with Unity?

YusufAltun
Huawei Developers
Published in
5 min readAug 5, 2020

Hello everyone,

In this article we are going to take a look at Huawei Mobile Services (HMS) integration Plugin for Unity. This article not going through the details of kits, we will focus on integration part

We will use account, In App purchases and push kit for this article. This plugin for now support five main kits.

Plugin Features :

1- Account Kit: Its provides developers with simple, secure, and quick sign-in and authorization functions. Instead of entering accounts and passwords and waiting for authorization, users can just tap the Sign In with HUAWEI ID button to quickly and securely sign in to your app. For details you read this article.

2- In App purchases: This service allows you to offer in-app purchases and facilitates in-app payment. Users can purchase a variety of virtual products, including one-time virtual products and subscriptions, directly within your app. For details you can read this article. Plugin supports the following types of products: Consumables, Non-consumables, Subscriptions

Integration Process

3- Huawei Ads: This Publisher Service utilizes Huawei’s vast user base and extensive data capabilities to deliver targeted, high quality ad content to users. With this service, your app will be able to generate revenues while bringing your users content which is relevant to them. For details you can read this article. Plugin supports the following types: Interstitial and rewarded videos

4- Push notifications: HUAWEI Push Kit is a messaging service provided by Huawei for developers. It establishes a messaging channel from the cloud to devices. By integrating HUAWEI Push Kit, developers can send messages to apps on users’ devices in real time. For details you can read this article.

5- Game kit: This kit provide player info, leaderboards and achievements.For details you can read this article.

HMS Plugin Work Flow:

HMS Plugin Schema

Prerequisites

Step 1

Step 2

This plugin have two branch for Unity versions.(version 2019 used)

  • Download plugin from this for Unity version 2019.x.
  • Download plugin from this for Unity version 2018.x .
Github package

Step 3

Import package to unity.

Import Package

Step 4

Update “AndroidManifest file” and “agconnect.json” file.

1- Open project_path\Assets\Plugins\Android\ AndoridManifest.

Update App ID, CP ID and package name, this informations exist in agconnect.json file.

AndroidManifest File

2- Open project_path\Assets\Huawei\agconnect.json replace with you downloanded from AGC.

Integration

This plugin have some demos scenes. You can drictly use this scenes. We will create empty scene, after that we will add objects.

1. Account kit:

  • Create empty object and add account manager script from Huawei package component. ( Object name is important, if you want to give diffrent name from “AccountManager” update ~~\Assets\Huawei\Account\AccountManager.cs -> line 11)
  • Add new script “AccountSignIn” to this object.
Account Manager Object

On AccountSignIn.cs:

Account Signin Class

We can create sign in and sign out buttons, this buttons call “AccountSignIn” functions.

SignIn Button Settings

2. In App purchases:

  • Create empty object ( Object name is important, if you want to give diffrent name from “IapManager” update ~~\Assets\Huawei\IAP\IapManager.cs -> line 11).
  • Add new script “IapController.cs” to this object.
  • Add Account kit manager to IapManager object, its needed for using IAP services.

In App Purchases have three type of product.

1- Type 0: Consumables

Description:Consumables are used once, are depleted, and can be purchased again.

Example:Extra lives and gems in a game.

Getting Consumables products from Huawei AGC with HMS Unity plugin:

IapManager consumable information function

2- Type 1: Non-consumables

Description: Non-consumables are purchased once and do not expire.

Example:Extra game levels in a game or permanent membership of an app

3- Type 2: Subscriptions

Description:Users can purchase access to value-added functions or content in a specified period of time. The subscriptions are automatically renewed on a recurring basis until users decide to cancel.

Example:Non-permanent membership of an app, such as a monthly video membership

On IAPController.cs

After getting all product informations from AGC, user can buy product with “BuyProduct” function with send product ids.

Buy Product Button Settings:

BuyProduct Button Listener
Backend function selection for button
Button event with Parameter

IapManager.cs

3. Push notifications:

First add your project PushKitManager prefab, this prefab using PushKitManager.cs for creating tokens.

For push kit, we have a two choice,

  • We can send notification to all devices without token.
  • We can get tokens from devices and we can use this token to send notification sepicified devices.

AGC server Push Scope for sending notification:

On PushKitManager.cs

Push Notification result:

Thank you for reading this article.

I hope this gives you a starting point for Huawei Mobile Services and Unity integration.

Links:

--

--