[Huawei Developers] Freemind R:
Introduction

In this article, we will learn how to add ADS into our Unity Game. Get paid to show relevant ads from over a million advertisers with HMS ADS in our Unity Games. Ads are an effective and easy way to earn revenue from your games. Ads Kit is a smart monetization platform for apps that helps you to maximize revenue from ads and in-app purchases. Thousands of Apps use HMS ADS Kit to generate a reliable revenue stream.

All you need to do is add the kit to your unity game, to place ads with just a few lines of code.

Implementation Steps

1. Creation of our App in App Gallery Connect

2. Evil Mind plugin Integration

3. Unity Configuration

4. Creation of the scene

5. Coding

6. Configuration for project execution

7. Final result

App Gallery Connect Configuration

Creating a new App in the App Gallery connect console is a fairly simple procedure but requires paying attention to certain important aspects.

Once inside the console we must create a project and to this project we must add an App.

When creating our App we will find the following form. It is important to take into account that the category of the App must be Game.

Once the App is created, it will be necessary for us to activate the Account Kit in our APIs, we can also activate the Game service if we wish.

HUAWEI Push Kit establishes a messaging channel from the cloud to devices. By integrating Push Kit, you can send messages to your apps on users' devices in real time.

Once this configuration is completed, it will be necessary to add the SHA-256 fingerprint, for this we can use the keytool command, but for this we must first create a keystore and for this we can use Unity.

Once the keystore is created we must open the console, go to the path where the keystore is located and execute the following code. Remember that to use this command you must have the Java JDK installed.

Once inside the route

Keytool -list -v -keystore yournamekey.keystore

This will give us all the information in our keystore, we obtain the SHA256 and add it to the App.

Unity Evil Mind Plugin configuration

We have concluded the creation of the App, the project and now we know how we can create a keystore and obtain the sha in Unity.

In case you have not done it now we must create our project in Unity once the project is created we must obtain the project package which we will use to connect our project with the AGC SDK. first of all let's go download the Evil Mind plugin.

GitHub - EvilMindDevs/hms-unity-plugin: The HMS Unity Plugin makes it easy to include Huawei Mobile Services into Unity-based games. Authentication, in-app purchases, push alerts, ads, and interaction with gaming services are just a few of the basic

The HMS Unity Plugin makes it easy to include Huawei Mobile Services into Unity-based games. Authentication, in-app purchases, push alerts, ads, and interaction with gaming services are just a few ...

github.com

In the link you can find the package to import it to Unity, to import it we must follow the following steps.

Download the .unity package and then import it into Unity using the package importer.

Once imported, you will have the Huawei option in the toolbar, we click on the option and add the data

from our App Gallery Console

Once we have imported the plugin we will have to add the necessary data from our App Gallery App and place it within the

required fields of the Unity plugin. Well, now we have our App Gallery App connected to the Unity project.

Now we can add the Push Notifications prefab to our scene remember that to do this we must create a new scene,

for this example we can use the scene that the plugin provides.

Unity Configuration

We have to remember that when we are using Unity is important to keep in mind that configurations needs to be done within the Engine so the apk runs correctly. In this section i want to detail some important configurations that we have to change for our Engine.

Switch Plaform.- Usually Unity will show us as default platform the PC, MAC so we have to change it for Android like in this picture.

Scripting Backend.- In order to run correctly the Scripting Backend must be change to IL2CPP, by default U

nity will have Mono as Scripting Backend so its important to chenge this information.

Minimun API Level.- Other configuration that needs to be done is Minimun API, we have to set it to API Level 21. otherwise the build wont work.

Creation of the scene

Within this step we must Create a new Scene From Scratch where we will have to add the following elements.

AdsManager.-The prefab that comes with the plugin

Canvas.- Where we will show the buttons to trigger Banners

EventSystem.- To Add input for Android Handheld

AdsManager.-Where the script to control the Ads Instances.

Coding

Lets check the Code of the Prefab that comes with the plugin. In this article i want to use Interstitial Ads so, first lets review what are the characteristics of these ads. Interstitial ads are full-screen ads that cover the interface of an app. Such an ad is displayed when a user starts, pauses, or exits an app, without disrupting the user's experience.

The code of the Instersticial Ad has the way to get an Instance of the Object.

public static InterstitalAdManager GetInstance(string name = "AdsManager") => GameObject.Find(name).GetComponent<InterstitalAdManager>();

As well we have a get Set to add the token Id of the Test or release Ad.

public string AdId
{
get => mAdId;
set
{
Debug.Log($"[HMS] InterstitalAdManager: Set interstitial ad ID: {value}");
mAdId = value;
LoadNextInterstitialAd();
}
}

This paramaters needs to be assign in the Script that controls the Ads behavior. Finally another important code that we have in this script is the listener of the Interstitial Ad. Methods to recognize Ad behaviour like Click, Close and Fail can be listen in this Section.

private class InterstitialAdListener : IAdListener
{
private readonly InterstitalAdManager mAdsManager;

public InterstitialAdListener(InterstitalAdManager adsManager)
{
mAdsManager = adsManager;
}

public void OnAdClicked()
{
Debug.Log("[HMS] AdsManager OnAdClicked");
mAdsManager.OnAdClicked?.Invoke();
}

public void OnAdClosed()
{
Debug.Log("[HMS] AdsManager OnAdClosed");
mAdsManager.OnAdClosed?.Invoke();
mAdsManager.LoadNextInterstitialAd();
}

public void OnAdFailed(int reason)
{
Debug.Log("[HMS] AdsManager OnAdFailed");
mAdsManager.OnAdFailed?.Invoke(reason);
}

public void OnAdImpression()
{
Debug.Log("[HMS] AdsManager OnAdImpression");
mAdsManager.OnAdImpression?.Invoke();
}

public void OnAdLeave()
{
Debug.Log("[HMS] AdsManager OnAdLeave");
mAdsManager.OnAdLeave?.Invoke();
}

public void OnAdLoaded()
{
Debug.Log("[HMS] AdsManager OnAdLoaded");
mAdsManager.OnAdLoaded?.Invoke();
}

public void OnAdOpened()
{
Debug.Log("[HMS] AdsManager OnAdOpened");
mAdsManager.OnAdOpened?.Invoke();
}
}

More details, you can check https://forums.developer.huawei.com/forumPortal/en/topic/0201461409178140023