👨‍💻Say Goodbye to Outdated Dependencies: The Transition from Unity’s HMS AGC Services to the HMS Unity Plugin

Hilal Tepecik
Huawei Developers
Published in
10 min readFeb 17, 2023

--

Image by 三峰 张 from Pixabay

Introduction✈️

With the introduction of the HMS Unity Plugin, developers can now access an even wider range of tools and features. However, if you previously relied on the outdated Unity asset store solution for HMS, you’ll need to transition to the new plugin. In this article, we’ll provide a step-by-step guide for removing the old solution and transitioning to the HMS Unity Plugin.

Unity’s HMS AGC Service

What is HMS Unity Plugin🥁

The HMS Unity Plugin is a powerful tool that provides access to the latest features and services from Huawei Mobile Services. It integrates seamlessly into your Unity project, giving you access to a range of features including account management, advertising, in-app purchases, and more. With the HMS Unity Plugin, you can take advantage of Huawei’s extensive developer resources and support, ensuring that your project runs smoothly and reliably.

HMS-UNITY-PLUGIN

Whether you’re just starting out or have been working in Unity for years, the HMS Unity Plugin is the perfect solution for anyone looking to enhance their mobile app with HMS Services.

In this article we will walk through the process of removing the outdated solution from Unity’s asset store and transitioning to the supported HMS Unity Plugin.

By making this switch, you’ll have access to a range of new and improved functions, including the latest updates from Huawei App gallery connect API’s. You’ll also be able to take advantage of the latest Account Kit, Ads Kit, and In-App Purchase Kit functions, allowing you to streamline your development process so; let’s get started!

Quick explanation, I will demonstrate the transition from the outdated solution in Unity’s asset store to the supported HMS Unity Plugin by using code examples from a real project.

PopTheBalloons Game Project

It’s a good starting point for those looking to transition. To help explain the process, I will provide links to two projects, one with the Unity solution and the other with the HMS Unity Plugin so you can see the difference in approach and implementation. Remember to adjust the code to suit your project and thoroughly test any changes before deployment.

Let’s have a look at the transition step-by-step. I will explain each step later with sample screenshots.

  1. Delete the Huawei Service Folder.🧼
  2. Disable conflicting codes.🎏
  3. Download the latest version of HMS Unity Plugin.📲
  4. Move the agconnect.json file to the streaming assets file.🛸
  5. Delete Android folder (Assets -> Plugin -> Android).📤
  6. Refactor your codes according to HMS Unity Plugin.📚

🧼Delete the Huawei Service Folder.🧼

The first step in removing the old solution is to delete the “Huawei Service” folder located under the Assets folder in your project.

Huawei Service Folder

Once this is done, you may run into conflicts as the project can no longer find the required dependencies.

Error Logs

🎏Disable conflicting codes🎏

To resolve these conflicts, you need to put all scripts that depend directly on the SDK in a command line to temporarily disable them.

For example, I’m going to delete my AccountTest.cs script as it is directly and completely dependent on the Asset Store Solution.

AccountTest.cs script

By doing this, AccountManager will throw an error because it cannot reach AccountTest.cs for the RegisterEvent() function.

AccountTest references

So I’ll put them in a comment line to remember where I call the Plugin in the future.

Account Manager refactoring

You can check which scripts are causing conflicts by looking at the Error Logs or separating dependent scripts from other scripts. By doing this, you can quickly identify and resolve any issues before moving on to the next step of integrating the HMS Unity Plugin.

Now I have other scripts for Ads kit. I’ll do the same by deleting a completely dependent code and disabling its references. For Ads Kit, AdsTest.cs and AdsListener are 2 fully dependent scripts. Let’s delete them.

AdsTest.cs and AdsListener.cs

UIController.cs gave me an error referencing AdsTest as expected. And I put it in a comment line to remember when I call Ads Kit again.

UIController reference check

Now let’s do the same for the IAP Kit. I have 2 completely dependent scripts. IAPTest.cs and IAPListener.cs. I will delete the references and script in IAPManager.cs.

IAPListener.cs andIAPTest.cs
IAPManager.cs reference check

I will also delete the LocationListener.cs file as I won’t need it for the HMS-Unity-Plugin.

Also make sure to delete all library calls for your script.

Deleting library calls

If you still encounter errors from unrelated files (e.g. Cinemachine or Plastic SCM), don’t worry.

Unrelated Errors

Restart your project, and these errors should go away. If Unity prompts you to enter safe mode after a reboot, click Ignore for this request. After restarting the project, check if any other scripts have dependencies and fix the issues accordingly.

📲Download Latest Version of HMS Unity Plugin.📲

After resolving all conflicts and dependencies, it’s time to download the latest version of HMS Unity Plugin. You can find the plugin here.

You should right click on Assets folder and select Import Package -> Custom package and select the Hms Unity package you downloaded.

HMS Unity Plugin Download

Also, don’t forget to import every dependency it has.

🛸Move the agconnect.json File to the Streaming Assets File.🛸

If it didn’t come with a Streaming Assets folder, you can create a new folder under assets and name it StreamingAssets. Make sure the name is exactly the same as “StreamingAssets”.

Because you have already registered as a developer on App Gallery Connect and own your app and project. You already have agconnect.json file. You can now choose either one;

  1. Move the agconnect.json file from Assets -> Plugin -> Android to Streaming Assets. or;
  2. Download agconnect.json from App Gallery Connect and place it in the StreamingAssets Folder.

📤Delete the Android File📤

Afterwards, you will no longer need an Android folder, so you can delete it. It should be under Assegts -> Plugin -> Android.

📚Refactor your codes according to HMS Unity Plugin.📚

Now we’re mostly done with the transport! Only the best part remains🧛🏻

Next, you must activate the kits you use in your project. Go to Editor -> Huawei -> Kit Settings and make sure Enable Plugin is turned on.

HMS Unity Plugin Kit Settings

And open the kits you use. 📖

HMS Unity Plugin Kit Settings

Now our project just uses HMS Unity Plugin and we can start rewriting your codes according to HMS-Unity-Plugin.

For this project I used 3 Kits that I will show;

  • Account Kit
  • Ads Kit
  • IAP Kit

Account Kit;

HMS Unity Plugin’s Account Kit provides a convenient and secure way for users to sign in to your app using their Huawei account. It eliminates the need for users to create and remember a separate username and password for your app.

HMS Account Kit

Let’s move on to building the HMS Unity Plugin’s Account Kit for Unity.

I’ll start by adding HMS libraries to our Account Manager script;

Library Calls

Since we no longer have AccountTest.cs, I will implement the AccountManager as it works with the HMS Unity Plugin.

Here is a brief overview of AccountManager.cs. Instead of adding Button Click Listeners, we will bind the buttons to the script in the editor to show the options.

Editör References

Here I added On Click Listener from the editor and I will delete Test Btn because we don’t need it anymore. I will do the same with the SignOut button and it has been added to our Accont Kit project!

Ads Kit;

Ads Kit is an essential part of HMS Unity Plugin and allows you to monetize your app through ads. With Ad Kit, you can easily integrate ads into your app and start generating revenue.

HMS Ads Kit

Before you begin, make sure you have the correct settings for Ads Kit.

Ads Kit Settings

If you remember, we deleted AdListener and AdsTest as they are completely dependent on Asset Store Solution. But we need a script to help us bridge the HMS Unity Plugin with the editor. For this purpose I will create AdsManager.cs and call relevant Ads from there.

This is the main AdsManager.cs we will use to get the ads we want. But in order to use it efficiently, we need to give the right references from other scripts.

Now remember the codes we put in the comment line. Now is their time. We should make our references where they are.

For example, instead of showing video ads here, we have enabled the ShowAds() method from the ad manager. If you look above, there are all kinds of advertisements you need. Also, we don’t need to enable Splash Name when using HMS Unity Plugin. This is an added feature on Start() but can be disabled at any time.

ShowAds() function

I think it’s enough for the ad kit. If you have any questions on behalf of AdsKit, you can download the project and check the final version.

IAP Kit;

The In-App Purchase (IAP) kit is a key component of the HMS Unity Plugin, designed to help developers easily implement in-app purchases in Unity projects. With the IAP kit, developers can easily manage their products and make the purchasing process simple and hassle-free for users.

HMS IAP Kit

Now, the use of IAP Kit has started on behalf of our projects. First you need to make sure IAP Kit is enabled and products are reported in both Kit Settings and App Gallery Connect.

HMS IAP products
IAP Ürünleri Unity Kit Settings

You must create a class of constants if they are declared correctly as you created them in App Gallery Connect. Also, don’t forget to check Initialize on Start.

We are ready to code!🦖

As you may recall, we had a script responsible for in-app purchases; The updated version of IAPManager.cs will be very different as it does not have enough coverage, but its scope will also be beyond Asset Store Solution.

It may seem complicated but please look at the bigger picture and try to understand the project as a whole.

Here is the similarity of the two Plugins. You can search for your product’s information in Asset Store Solution as follows;

  Debug.Log("product info currency is " + info.getCurrency());
Debug.Log("product info micros price is " + info.getMicrosPrice());
Debug.Log("product info getOfferUsedStatus is " + info.getOfferUsedStatus());
Debug.Log("product info getOriginalLocalPrice is " + info.getOriginalLocalPrice());
Debug.Log("product info getOriginalMicroPrice is " + info.getOriginalMicroPrice());
Debug.Log("product info price is " + info.getPrice());
Debug.Log("product info price type is " + info.getPriceType());
Debug.Log("product info product desc is " + info.getProductDesc());
Debug.Log("product info product id is " + info.getProductId());
Debug.Log("product info product name is " + info.getProductName());

The equivalents of these parameters in the HMS Unity Plugin;

  Debug.Log("product info currency is " + info.Currency());
Debug.Log("product info micros price is " + info.MicrosPrice());
Debug.Log("product info getOfferUsedStatus is " + info.OfferUsedStatus());
Debug.Log("product info getOriginalLocalPrice is " + info.OriginalLocalPrice());
Debug.Log("product info getOriginalMicroPrice is " + info.OriginalMicroPrice());
Debug.Log("product info price is " + info.Price());
Debug.Log("product info price type is " + info.PriceType());
Debug.Log("product info product desc is " + info.ProductDesc());
Debug.Log("product info product id is " + info.ProductId());
Debug.Log("product info product name is " + info.ProductName());

As you can see, migrating from Unity’s Asset Store Solution to the HMS Unity Plugin is very easy and straightforward.

If you have followed the steps correctly, you should be ready for a successful purchase. Done!

Conclusion🌅

In conclusion, switching to the HMS Unity Plugin is a crucial step in ensuring that your project is equipped with the latest features and tools. Removing the outdated solution from the Unity asset store may seem like a daunting task, but by following the steps outlined in this article, you’ll be able to seamlessly transition to the HMS Unity Plugin without any hassles.

Thank you for reading this article, we hope it was helpful in guiding you through the process of removing dependencies and migrating to the HMS Unity Plugin. We hope you find all the latest features and tools useful to enhance your development experience.🫀

See you in a new article!

References:

Asset Store Solution Version of the project;

Completed project with the HMS Unity Plugin!

--

--

Hilal Tepecik
Huawei Developers

Game and SDK Developer @Huawei - Unity, Unreal, HMS Unity Plugin, XR Interactions Toolkit