[Huawei Developers] Sherlock F:
Unity & HMS account kit integration.

In this article, we will integrate HMS account kit with Unity. We will perform Login & Logout.

Requirements ->

Unity Editor

Huawei device

Visual Studio

Output --> Upon succeful login User will get display name and other details, On UI i am populating DisplayName like below.

Step 1	Create Unity 3d project

Step 2	Register project on HMS Console as Game.

(Dont forget to put SHA key from keystore file)

Step 3	Collect below information

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200821214212.87831843263199105962131517711095:50510908074020:2800:B130FE14325D0326FB7B359F317B3221CFE236F225FA2F7040F7A76DA713F69F.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200821214212.87831843263199105962131517711095:50510908074020:2800:B130FE14325D0326FB7B359F317B3221CFE236F225FA2F7040F7A76DA713F69F.png[/IMG]

Step 4	Download HMS Unity Plugin (HMS Unity Plugin v1.1.2) from

https://github.com/EvilMindDevs/hms-unity-plugin/releases

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200821214003.34793314943896787290302545167609:50510908074020:2800:339DF2788952C9AEE0904890BE58329740BA573C0060F92E330C5AFBE925DDEB.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200821214003.34793314943896787290302545167609:50510908074020:2800:339DF2788952C9AEE0904890BE58329740BA573C0060F92E330C5AFBE925DDEB.png[/IMG]

Step 5	Import this plugin in Unity project like show below

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200823085250.98516505156447312070592591923262:50510908074020:2800:01FE0A56D7638C9DCB88AA8F392A757954CD837087B3A9B65B6FF77A952442D8.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200823085250.98516505156447312070592591923262:50510908074020:2800:01FE0A56D7638C9DCB88AA8F392A757954CD837087B3A9B65B6FF77A952442D8.png[/IMG]

Step 6	Additoinal Configuration using HMS Plugin, click on Huawei -> App Gallery

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200823085633.44320397935066168999945226963898:50510908074020:2800:86A05AA28112C0B0C01A38E0E91F10AD57772580940587C9A5140199D09A9421.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200823085633.44320397935066168999945226963898:50510908074020:2800:86A05AA28112C0B0C01A38E0E91F10AD57772580940587C9A5140199D09A9421.png[/IMG]

Step 7

in step 3 we collected information which we need to fill here in Huawei Plugin Editor, like show below

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200823085807.32071821866068808883696478883491:50510908074020:2800:7F3D382C4894BE186A93E6260567BFC584D04FFC96C679E469F5D06382713B80.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200823085807.32071821866068808883696478883491:50510908074020:2800:7F3D382C4894BE186A93E6260567BFC584D04FFC96C679E469F5D06382713B80.png[/IMG]

Step 8	Once information is filled a button will appear "Configure Manifest", click on that and close editor

Step 9	Verify details in manifest file, you can find this file in below location

Assets -> Plugins -> Android -> AndroidManifest.xml

We need to verify above filled details in this file like show below

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200823091230.31629212912583849918199166470802:50510908074020:2800:AD5A4E8B37F57793F6BC3B0838085FE034B4DFAA69F18232405B340F0009B334.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200823091230.31629212912583849918199166470802:50510908074020:2800:AD5A4E8B37F57793F6BC3B0838085FE034B4DFAA69F18232405B340F0009B334.png[/IMG]

Step 10	Update & Verify package name like below

File -> BuildSettings -> Android -> Player Settings -> Other Settings

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200823095224.88547400489023317412954034733750:50510908074020:2800:32C2A88C9164AF3DC720EF2F8DDE6433D4430C180378014636163F567E3A2C9C.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200823095224.88547400489023317412954034733750:50510908074020:2800:32C2A88C9164AF3DC720EF2F8DDE6433D4430C180378014636163F567E3A2C9C.png[/IMG]

Step 11	Install Unity Remote apk in your huawei device for running and debugging project

Please use custom keystore

Build the project and try to run -> if it gives error like manifest merger failed, which i got, open manifest file in text editor and fix it, if it has duplicate providers, once done you can see ur project running in huawei device

Step 12	HMS Plugin apis will be available as soon as prefab is added to the scene like below

Where to find prefab -> Assets -> Huawei -> Prefabs

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200823092949.91035543795842392753925485371154:50510908074020:2800:2E235FC697D0D78A8A3D9E135476E9A9AE35F25A61BE9849F738022DA7A0A5D4.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/002/032/901/2640091000002032901.20200823092949.91035543795842392753925485371154:50510908074020:2800:2E235FC697D0D78A8A3D9E135476E9A9AE35F25A61BE9849F738022DA7A0A5D4.png[/IMG]

Step 13	Scripting

I am going to create 2 scripts, both are conncted on my Player (U can connect on any element)

PlayerController - I used this to initalize HMS Account Kit

UpdateDetails - to update user name on canvas received from HMS account service.

PlayerController.cs

using System.Collections;
using System.Collections.Generic;
using HmsPlugin;
using UnityEngine;

public class PlayerController : MonoBehaviour
{

public float runSpeed;
Rigidbody myRb;
Animator myAnim;
bool facingRight;

private AccountManager accountManager;
UpdateDetails updateDetails;

// Start is called before the first frame update
void Start()
{
updateDetails = GetComponent<UpdateDetails>();
myRb = GetComponent<Rigidbody>();
myAnim = GetComponent<Animator>();
facingRight = true;

accountManager = AccountManager.GetInstance(); // Initialization of account manager
accountManager.OnSignInSuccess = OnLoginSuccess;
accountManager.OnSignInFailed = OnLoginFailure;
Debug.LogWarning("kamal Start");

accountManager.SignIn(); // login
}

public void OnLoginSuccess(HuaweiMobileServices.Id.AuthHuaweiId authHuaweiId)
{
//loggedInUser.text = string.Format(LOGGED_IN, authHuaweiId.DisplayName);
Debug.LogWarning("kamal OnLoginSuccess-->"+ authHuaweiId.DisplayName);
updateDetails.updateUserName("Welcome "+authHuaweiId.DisplayName);
}

public void OnLoginFailure(HuaweiMobileServices.Utils.HMSException error)
{
//loggedInUser.text = LOGIN_ERROR;
Debug.LogWarning("kamal OnLoginSuccess");
updateDetails.updateUserName("error in login-- " + error.Message);
}

// Update is called once per frame
void Update()
{

}
}

UpdateDetails.cs -> update details on UI Canvas

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class UpdateDetails : MonoBehaviour
{
public Text userName;

// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{

}

public void updateUserName(string name) {

Debug.LogWarning("kamal updateUserName-->" + name);
userName.text = name;
}
}

Result -> User name is shown on UI after login using HMS account kit.

What about Logout -> User below code snipped

accountManager.SignOut();