[Huawei Developers] ask011:
Introduction

In this article, we will cover Integration of Huawei Kit in Unity Project using Official Plugin (Huawei HMS Core App Services). Here we will cover below Kits. With Huawei Game Service, you will have access to a range of development capabilities. You can promote your game quickly and efficiently to Huawei's vast user base by users sign in with their Huawei IDs. You can also use the service to quickly implement achievements, game events, and game addiction prevention functions, build basic game capabilities at a low cost, and perform in-depth game operations based on user and content localization.

Game Service provides the following basic functions for your game apps, with which you can quickly build basic game capabilities.

Game Service Login

Achievements

Leader Board Data

Current Player Info

Game Event begin and end

Development Overview

You need to install Unity software and I assume that you have prior knowledge about the unity and C#.

Hardware Requirements

A computer (desktop or laptop) running Windows 10.

A Huawei phone (with the USB cable), which is used for debugging.

Software Requirements

Java JDK installation package.

Unity software installed.

Visual Studio/Code installed.

HMS Core (APK) 4.X or later.

Follows the steps.

1. Create Unity Project.

Open unity Hub.

Click NEW, select3D, Project Name and Location.

Click CREATE, as follows:

View attachment 5322437

2. Click Asset Store, search Huawei HMS Core App Servicesand click Import, as follows.

View attachment 5322439

3. Once import is successful, verify directory in Assets> Huawei HMS Core App Services path, as follows.

View attachment 5322441

4. Choose Edit > Project Settings > Player and edit the required options in Publishing Settings, as follows.

View attachment 5322447

5. Generate a SHA-256 certificate fingerprint.

To generating SHA-256 certificate fingerprint use below command.

keytool -list -v -keystore D:\Unity\projects_unity\file_name.keystore -alias alias_name

View attachment 5322449

6. Download agconnect-services.json and copy and paste to Assets > Plugins > Android, as follows.

View attachment 5322451

7. Choose Project Settings > Player and update package name.

View attachment 5322453

8. Open LauncherTemplate.gradle and add below lines.

apply plugin: 'com.huawei.agconnect'

implementation 'com.huawei.agconnect:agconnect-core:1.4.2.300'
implementation 'com.huawei.hms:base:5.0.5.300'
implementation 'com.huawei.hms:hwid:5.0.5.301'
implementation 'com.huawei.hms:game:5.0.4.302'

9. Open "baseProjectTemplate.gradle" and add lines, as follows.

classpath 'com.huawei.agconnect:agcp:1.4.1.300'

maven {url 'https://developer.huawei.com/repo/'}

10. Open "mainTemplate.gradle" and add lines, as follows.

implementation 'com.huawei.agconnect:agconnect-core:1.4.2.300'
implementation 'com.huawei.hms:base:5.0.5.300'
implementation 'com.huawei.hms:hwid:5.0.5.301'
implementation 'com.huawei.hms:game:5.0.4.302'

11. Open AndroidManifest file and add below permissions.

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

12. Create Scripts folder and create a class.

HmsGameService.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
using HuaweiService;
using UnityEngine.HuaweiAppGallery;
using UnityEngine.HuaweiAppGallery.Listener;
using UnityEngine.HuaweiAppGallery.Model;
using UnityEngine.SceneManagement;

public class HmsGameService : MonoBehaviour
{
// Start is called before the first frame update
private ILoginListener iLoginListener = new LoginListener();
public Text userName;
public Button ButtonLogout;
public Button ButtonLogin;
public Button ButtonGetPlayerInfo;
static string user,sessionId;
static string playerId,guid;
private static List<string> achievementIds = new List<string>();

void Start()
{
HuaweiGameService.AppInit();
}
// Update is called once per frame
void Update()
{
StartCoroutine(UpdateUICoroutine());
}

public void GetAchievements()
{
string guid = System.Guid.NewGuid().ToString();
HuaweiGameService.GetAchievementList(true, new MyGetAchievementListListener());
}

public void GetLeaderBoardData(){
string guid = System.Guid.NewGuid().ToString();
HuaweiGameService.GetAllLeaderboardsIntent(new MyGetLeaderboardIntentListener());
}

public class MyGetLeaderboardIntentListener : IGetLeaderboardIntentListener
{
public void OnSuccess(AndroidJavaObject intent)
{
var msg = "Get leader board intent succeed";
Debug.Log(msg);
user = msg;
}
public void OnFailure(int code, string message)
{
var msg = "Get leaderboard failed, code:" + code + " message:" + message;
Debug.Log(msg);
user = msg;
}
}
public void GetInfo(){
HuaweiGameService.GetCurrentPlayer(true, new MyGetCurrentPlayer());
}
public class MyGetAchievementListListener : IGetAchievementListListener
{
public void OnSuccess(List<Achievement> achievementList)
{
string message = "Achievement count :" + achievementList.Count + "\n";
achievementIds = new List<string>();
foreach (var achievement in achievementList)
{
message += string.Format(

"id:{0}, type:{1}, name:{2}, description:{3} \n",

achievement.AchievementId,

achievement.Type,

achievement.Name,

achievement.Description
);
achievementIds.Add(achievement.AchievementId);
}
user = message;
}

public void OnFailure(int code, string message)
{
string msg = "get achievement list failed, code:" + code + " message:" + message;
user = msg;
}

}

void display(){
userName.text = user;
}

void display(string name)
{
userName.text = name;
}
IEnumerator UpdateUICoroutine() {
//yield on a new YieldInstruction that waits for 5 seconds.
yield return new WaitForSeconds(3);
display();
}

public void onLoginClick()
{
Debug.Log("starting Init");
HuaweiGameService.Init();
Debug.Log("starting login");
HuaweiGameService.Login(iLoginListener);
Debug.Log("finshed login");
}

public class LoginListener : ILoginListener
{
public void OnSuccess(SignInAccountProxy signInAccountProxy)
{
user = "Wel-come "+signInAccountProxy.DisplayName;
}
public void OnFailure(int code, string message)
{
string msg = "login failed, code:" + code + " message:" + message;
Debug.Log(msg);
}
public void OnSignOut(){

}
}

public class MyGetCurrentPlayer : IGetPlayerListener
{
public void OnSuccess(Player player)
{
string msg = "Player ID: " + player.PlayerId +"\nPlayer Name : "+ player.DisplayName;
playerId = player.PlayerId;
Debug.Log(msg);
user = msg;
}

public void OnFailure(int code, string message)
{
string msg = "Get Current Player failed, code:" + code + " message:" + message;
Debug.Log("OnFailure :"+msg);
user = msg;
}
}

public class MyLeaderboardSwitchStatus : ILeaderboardSwitchStatusListener
{
public void OnSuccess(int statusValue)
{
string msg = "LeaderboardSwitchStatus Success: " + statusValue;
Debug.Log(msg);
user = msg;

}

public void OnFailure(int code, string message)
{
string msg = "LeaderboardSwitchStatus failed, code:" + code + " message:" + message;
Debug.Log(msg);
user = msg;
}
}

public class MySubmitPlayerEventBegin : ISubmitPlayerEventListener
{
public void OnSuccess(string jsonRequest)
{
string msg = "submitPlayerEventBegin Success, player info: " + jsonRequest;
ConvertMessageData data = JsonUtility.FromJson<ConvertMessageData>(jsonRequest);
Debug.Log(msg);
sessionId = data.transactionId;
if(sessionId !=null){
HuaweiGameService.GetPlayerExtraInfo(sessionId, new MyGetPlayerExtraInfo());
}
}

public void OnFailure(int code, string message)
{
string msg = "submitPlayerEventBegin failed, code:" + code + " message:" + message;
Debug.Log(msg);
}

public class ConvertMessageData
{
public string transactionId;
}
}

public class MyGetPlayerExtraInfo : IGetPlayerExtraInfoListener
{
public void OnSuccess(AndroidJavaObject jo)
{
string msg = "getPlayerInfo Success, player info: " + jo.ToString();
Debug.Log(msg);
}
public void OnFailure(int code, string message)
{
string msg = "getPlayerInfo failed, code:" + code + " message:" + message;
Debug.Log(msg);
user = msg;
}
public void OnSuccess(PlayerExtraInfo playerExtraInfo)
{
string msg = "getPlayerInfo Success, player info: " + playerExtraInfo.ToString();
Debug.Log(msg);
user = msg;
}

}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- GENERATED BY UNITY. REMOVE THIS COMMENT TO PREVENT OVERWRITING WHEN EXPORTING AGAIN-->
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.unity3d.player"
xmlns:tools="http://schemas.android.com/tools">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application>
<activity android:name="com.unity3d.player.UnityPlayerActivity"
android:theme="@style/UnityThemeSelector">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="unityplayer.UnityActivity" android:value="true" />
</activity>
</application>
</manifest>

13. Follow the steps, as shown in image:

a. Assign HmsGameService script to Canvas.

b. Select Button and add onclick event.

c. Assign button to button handler.

View attachment 5322465

14. Onclick Button Handler you find your script HmsGameService (As per your script name) and attach method as per below screen shot.

View attachment 5322467

15. To build apk and run in device, choose File > Build Settings > Build for apk or Build and Run into connected device.

View attachment 5322469

Result

1. Click on Login Button you can see, it will login into game service as per below screenshot.

View attachment 5322471

2. Click on Player Info, Leader Board and Achievement button you and see result as per below screenshot.

View attachment 5322473

View attachment 5322477

View attachment 5322479

Tips and Tricks

Always use the latest version of the library.

Add agconnect-services.json file without fail.

Add SHA-256 fingerprint without fail.

Make sure dependencies added in build files.

Add Achievements and LeaderBoard details before run.

Conclusion

We have learnt integration of HMS Game Service Kit into Unity Game development. You have successfully built a game app and learned how to.

Use Huawei Game Service.

Use Huawei Game Service to develop the game sign-in function.

Use Huawei Game Service to develop the game addiction prevention function.

Thanks for reading the article, please do like and comment your queries or suggestions.

References

Game Service: https://developer.huawei.com/consum.../HMSCore-Guides/introduction-0000001050121216

Unity Manual : https://docs.unity.cn/cn/Packages-cn/com.unity.huaweiservice@1.3/manual/gameservices.html