[Huawei Developers] shikkerimath:
This document describes how to integrate Ads Kit using the official Unity asset. After the integration, your app can use the services of this Kit on HMS mobile phones.

For details about Ads Kit, please visit HUAWEI Developers.

1.1 Restrictions​

1.1.1 Ads Supported by the Official Unity Asset​

The official asset of version 1.3.4 in Unity supports interstitial ads and rewarded ads of Ads Kit.

Note: To support other types of ads, you can use the Android native integration mode. This document will take the banner ad as an example to describe such integration.

1.1.2 Supported Devices and Unity Versions​

View attachment 5251601

Note: If the version is earlier than 2018.4.25, you can manually import assets. If there are any unknown build errors that are not caused by the AfterBuildToDo file, upgrade your Unity.

1.1 Preparations​

1.1.1 Prerequisites​

l HMS Core (APK) 4.0.0.300 or later has been installed on the device. Otherwise, the APIs of the HUAWEI Ads SDK, which depend on HMS Core (APK) 4.0.0.300 or later, cannot be used.

l You have registered as a Huawei developer and completed identity verification on HUAWEI Developers. For details, please refer to Registration and Verification.

You have created a project and add an app to the project in AppGallery Connect by referring to Creating an AppGallery Connect Project and Adding an App to the Project.

For details about applying for a formal ad slot, please visit HUAWEI Developers.

1.1.2 Importing Unity Assets​

1. Open Asset Store in Unity.

Go to Window > Asset Store in Unity.

View attachment 5251603

2. Search for the Huawei HMS AGC Services asset. Download and then import it.

View attachment 5251605

3. Import the asset to My Assets, with all services selected.

View attachment 5251607

4. Change the package name.

Go to Edit > Project Settings> Player > Android > Other Settings in Unity, and then set Package Name.

The default package name is com.${Company Name}.${Product Name}. You need to change the package name, and the app will be released to AppGallery with the new name.

View attachment 5251611

View attachment 5251613

1.2.3 Generating .gradle Files​

1. Enable project gradle.

Go to Edit > Project Settings > Player in Unity, click the Android icon, and go to Publishing Settings > Build.

Enable Custom Main Manifest.

Enable Custom Main Gradle Template.

Enable Custom Launcher Gradle Template.

Enable Custom Base Gradle Template.

View attachment 5251615

View attachment 5251617

2. Generate a signature.

You can use an existing keystore file or create a new one to sign your app.

Go to Edit > Project Settings > Player in Unity, click the Android icon, and go to Publishing Settings > Keystore Manager.

Then, go to Keystore... > Create New.

View attachment 5251619

Enter the password when you open Unity. Otherwise, you cannot build the APK.

1.1.1 Configuring .gradle Files​

1. Configure the BaseProjectTemplate.gradle file.

Configure the Maven repository address.

<p style="line-height: 1.5em;">buildscript {
repositories {**ARTIFACTORYREPOSITORY**
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
}
repositories {**ARTIFACTORYREPOSITORY**
google()
jcenter()
maven { url 'https://developer.huawei.com/repo/' }
flatDir {
dirs "${project(':unityLibrary').projectDir}/libs"
}
}
2. Configure the launcherTemplate.gradle file.
Add dependencies.
apply plugin: 'com.android.application'
apply plugin: 'com.huawei.agconnect'
dependencies {
implementation project(':unityLibrary')
implementation 'com.huawei.hms:ads-lite:13.4.29.303'
}</p>

1.2 App Development with the Official Asset​

1.2.1 Rewarded Ads​

Rewarded ads are full-screen video ads that allow users to view in exchange for in-app rewards.

1.2.1.1 Sample Code​

<p style="line-height: 1.5em;">using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using HuaweiHms;
namespace Demo
{
public class RewardDemo : MonoBehaviour
{
public void LoadRewardAds()
{
// Create a RewardAd object.
// "testx9dtjwj8hp" is the test ad slot. You can use it to perform a test. Replace the test slot with a formal one for official release.
RewardAd rewardAd = new RewardAd(new Context(), "testx9dtjwj8hp");
AdParam.Builder builder = new AdParam.Builder();
AdParam adParam = builder.build();
// Load the ad.
rewardAd.loadAd(adParam, new MRewardLoadListener(rewardAd));
}
}
// Listen for ad events.
public class MRewardLoadListener:RewardAdLoadListener
{
private RewardAd ad;
public rewardLoadListener(RewardAd _ad)
{
ad = _ad;
}
public override void onRewardAdFailedToLoad(int arg0)
{
}
public override void onRewardedLoaded()
{
ad.show(new Context(),new RewardAdStatusListener());
}
}
}</p>

1.3.1.2 Testing the APK​

Go to File > Build Settings > Android, click Switch Platform and then Build And Run.

View attachment 5251623

1.3.2 Interstitial Ads​

1.3.2.1 Sample Code​

<p style="line-height: 1.5em;">using UnityEngine;
using HuaweiService;
using HuaweiService.ads;
namespace Demo
{
public class interstitialDemo : MonoBehaviour
{
public void LoadImageAds()
{
// Create an ad object.
// "testb4znbuh3n2" and "teste9ih9j0rc3" are test ad slots. You can use them to perform a test. Replace the test slots with formal ones for official release.
InterstitialAd ad = new InterstitialAd(new Context());
ad.setAdId("teste9ih9j0rc3");
ad.setAdListener(new MAdListener(ad));
AdParam.Builder builder = new AdParam.Builder();
AdParam adParam = builder.build();
// Load the ad.
ad.loadAd(adParam);
}
public void LoadVideoAds()
{
InterstitialAd ad = new InterstitialAd(new Context());
ad.setAdId("testb4znbuh3n2");
ad.setAdListener(new MAdListener(ad));
AdParam.Builder builder = new AdParam.Builder();
ad.loadAd(builder.build());
}
public class MAdListener : AdListener
{
private InterstitialAd ad;
public MAdListener(InterstitialAd _ad) : base()
{
ad = _ad;
}
public override void onAdLoaded()
{
// Display the ad if it is successfully loaded.
ad.show();
}
public override void onAdFailed(int arg0)
{
}
public override void onAdOpened()
{
}
public override void onAdClicked()
{
}
public override void onAdLeave()
{
}
public override void onAdClosed()
{
}
}
}
}</p>

1.4 App Development with Android Studio​

1.4.1 Banner Ads​

1.4.1.1 Loading Banner Ads on a Page as Required​

AndroidJavaClass javaClass = new AndroidJavaClass("com.unity3d.player.UnityPlayerActivity");

javaClass.CallStatic("loadBannerAds");

1.4.1.2 Exporting Your Project from Unity​

Go to File > Build Settings > Android and click Switch Platform. Then, click Export Project, select your project, and click Export.

View attachment 5251627

1.4.1.3 Integrating the Banner Ad Function in Android Studio​

Open the exported project in Android Studio.

Add implementation 'com.huawei.hms:ads-lite:13.4.29.303' to build.gradle in the src directory.

View attachment 5251631

1. Add code related to banner ads to UnityPlayerActivity.

a. Define the static variable bannerView.

<p style="line-height: 1.5em;">private static BannerView
bannerView
;</p>

b. Add the initialization of bannerView to the onCreate method.

<p style="line-height: 1.5em;">bannerView = new BannerView(this);
bannerView.setAdId("testw6vs28auh3");
bannerView.setBannerAdSize(BannerAdSize.BANNER_SIZE_360_57);
mUnityPlayer.addView(bannerView);</p>

c. Add the following static method for loading ads in the Android Studio project, and then build and run the project.

<p style="line-height: 1.5em;">public static void loadBannerAds()
{ // "testw6vs28auh3" is a dedicated test ad slot ID. Before releasing your app, replace the test ad slot ID with the formal one.
AdParam adParam = new AdParam.Builder().build();
bannerView.loadAd(adParam);
}</p>

d. If banner ads need to be placed at the bottom of the page, refer to the following code:

<p style="line-height: 1.5em;">// Set up activity layout.
@Override protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
String cmdLine = updateUnityCommandLineArguments(getIntent().getStringExtra("unity"));
getIntent().putExtra("unity", cmdLine);
RelativeLayout relativeLayout = new RelativeLayout(this);
mUnityPlayer = new UnityPlayer(this, this);
setContentView(relativeLayout);
relativeLayout.addView(mUnityPlayer);
mUnityPlayer.requestFocus();
bannerView = new BannerView(this);
bannerView.setAdId("testw6vs28auh3");
bannerView.setBannerAdSize(BannerAdSize.BANNER_SIZE_360_57);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); relativeLayout.addView(bannerView,layoutParams);
}
public static void LoadBannerAd() {
// "testw6vs28auh3" is a dedicated test ad slot ID. Before releasing your app, replace the test ad slot ID with the formal one.
AdParam adParam = new AdParam.Builder().build();
bannerView.loadAd(adParam);
bannerView.setAdListener(new AdListener()
{
@Override
public void onAdFailed(int errorCode)
{
Log.d("BannerAds" ,"error" + errorCode);
}
});
}</p>

1.5 FAQs​

1. If an error indicating invalid path is reported when you export a project from Unity, change the export path to Downloads or Desktop.

2. Unity of a version earlier than 2018.4.25 does not support asset download from Asset Store. You can download the asset using Unity of 2018.4.25 or a later version, export it, and then import it to Unity of an earlier version.

More Information

To join in on developer discussion forums

To download the demo app and sample code

For solutions to integration-related issues

Checkout in forum