Intermediate: How to integrate Huawei Auth Service in Unity

Lokesh Suryan
5 min readApr 23, 2021

--

Introduction

In this article, we will cover how to verify Phone Number and Anonymous Account Login using Huawei Auth Service in Unity Project using Official Plugin (Huawei HMS Core App Services). AppGallery Connect provides a cloud-based Auth Service and SDKs to help you quickly build a secure and reliable user authentication system for your apps to verify user identity.

The AppGallery Connect Auth service supports multiple authentication methods and is seamlessly integrated with other server less services to secure user data based on simple rules that you have defined.

Key Functions

Using the AppGallery Auth Service SDK, you can integrate one or more of the following authentication methods into your app for achieving easy and efficient user registration and sign-in.

  • Self-owned account: Your self-owned account is used to support the access of your existing authentication system, so that your existing users can access other server less services in a secure manner.
  • Anonymous account: Anonymous accounts can be used to access your apps as visitors. The Auth service can assign user IDs to your app visitors, so that they can access other server less services in a secure manner. A visitor can be registered as a formal user and retain the original user ID to ensure service continuity.
  • Third-party accounts: AppGallery Connect allows user identity to be verified by third-party authentication services. The AppGallery Auth Service SDK supports the following accounts for user identity verification:

1. HUAWEI account

2. HUAWEI Game Service account

3. Phone number

4. Email account

5. WeChat account

6. Weibo account

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, select 3D, Project Name and Location.
  • Click CREATE, as follows:

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

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

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

5. Generate a SHA-256 certificate fingerprint.

To generating SHA-256 certificate fingerprint use below command

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

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

8. Open LauncherTemplate.gradle and add below line.

9. Open AndroidManifest file and add below permissions.

10. Open “baseProjectTemplate.gradle” and add lines, as follows.

11. Open “mainTemplate.gradle” and add lines like shown below.

12. Create MainActivity.java class inside Plugin > Android folder.

MainActivity.java

Create Scripts folder and create a class.

HMSAuthService.cs

AndroidManifest.xml

13. Follow the steps, as shown in image:

a. Assign Ads script to Canvas.

b. Select Button and add onclick event

c. Assign all button to button handler as per your requirements.

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

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

Result

  1. Click on Anonymous Login, send OTP and Verify button you can see below results.

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.
  • Make sure that you enabled the auth service in AG-Console.
  • Make sure that you enabled the Authentication mode in Auth Service.

Conclusion

In this article, we have learnt integration of Huawei Auth Service-AGC anonymous account login and mobile number verification through OTP in Unity Game development. Auth Service provides secure and reliable user authentication system to your application.

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

References

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

Auth Service:-https://developer.huawei.com/consumer/en/doc/development/AppGallery-connect-Guides/agc-auth-introduction-0000001053732605?ha_source=hms1

Original Source:-https://forums.developer.huawei.com/forumPortal/en/topic/0202544702082820244?ha_source=hms1

For more details, you can visit to HMS Core GitHub organization (https://github.com/HMS-Core) to download demos and sample codes.

--

--

No responses yet