[Huawei Developers] Sherlock F:
In this post, I will describe how to load and show the Interstitial Ad.

Creating an Interstitial Ad Object

To create an InterstitialAd object, you need to initialize an InterstitialAd object of the AndroidJavaObject type and set the slot ID for the interstitial ad.

In the InterstitialTest.cs script, the interstitial ad proxy class mHwInterstitialAd in Android is instantiated through reflection to initialize the interstitial ad object InterstitialAd in the Ads SDK.

public class InterstitialTest : MonoBehaviour
{
...
private AndroidJavaObject mHwInterstitialAd;
// testb4znbuh3n2 indicates a test ad slot ID.
private const string adId = "testb4znbuh3n2";

...
private void handleRequestAd()
{
// Processing after clicking.
...
AndroidJavaClass playerClass = new AndroidJavaClass(Constant.UnityActivityClassName);
AndroidJavaObject activity = playerClass.GetStatic<AndroidJavaObject>("currentActivity");
mHwInterstitialAd = new AndroidJavaObject(Constant.InterstitialName, activity);
...
mHwInterstitialAd.Call("setAdId", adId);
...
}
...
}
...

From the Android project, you can define InstertialAdProxy class to provide methods for setting ad id.

class InterstitialAdProxy(private val context: context) {

private val mInterstitialAd: InterstitialAd
private var mAdListener: IAdStatusListener? = null
private val mMainThreadHandler = Handler(Looper.getMainLooper())

init {
mInterstitialAd = InterstitialAd(mContext)
}

fun setAdId(adId: String?) {
mInterstitialAd.setAdId(adId)
}
...
}

To call an Android API, you need to specify the path of the package name InterstitialName in the Android library project. The following shows the InterstitialName setting.

public class Constant
{
...
public const string InterstitialName = "com.huawei.hms.ads.unityhwadlib.adproxy.InterstitialAdProxy";
...
}

Setting an Ad Event Listener

We need to define a callback interface AdStatusListener that inherits AndroidJavaProxy in Unity to implement the interaction between the Unity callback function and Android. The event types of this interface are the same as those of the callback function interface IAdStatusListener in the Android project.

public class AdLoadErrorCodeEventArgs : EventArgs
{
public int ErrorCode { get; set; }
}

public class AdStatusListener : AndroidJavaProxy
{
public event EventHandler<EventArgs> mOnAdClosed;

public event EventHandler<AdLoadErrorCodeEventArgs> mOnAdFailed;

public event EventHandler<EventArgs> mOnAdLeftApp;

public event EventHandler<EventArgs> mOnAdOpened;

public event EventHandler<EventArgs> mOnAdLoaded;

public event EventHandler<EventArgs> mOnAdClicked;

public event EventHandler<EventArgs> mOnAdImpression;

public AdStatusListener() : base(Constant.AdStatusListenerName) {}

public void onAdClosed()
{
if (mOnAdClosed != null)
{
mOnAdClosed(this, EventArgs.Empty);
}
}

public void onAdFailed(int errorCode)
{
if (mOnAdFailed != null)
{
AdLoadErrorCodeEventArgs args = new AdLoadErrorCodeEventArgs()
{
ErrorCode = errorCode
};
mOnAdFailed(this, args);
}
}

public void onAdLeftApp()
{
if (mOnAdLeftApp != null)
{
mOnAdLeftApp(this, EventArgs.Empty);
}
}

public void onAdOpened()
{
if (mOnAdOpened != null)
{
mOnAdOpened(this, EventArgs.Empty);
}
}

public void onAdLoaded()
{
if (mOnAdLoaded != null)
{
mOnAdLoaded(this, EventArgs.Empty);
}
}

public void onAdClicked()
{
if (mOnAdClicked != null)
{
mOnAdClicked(this, EventArgs.Empty);
}
}

public void onAdImpression()
{
if (mOnAdImpression != null)
{
mOnAdImpression(this, EventArgs.Empty);
}
}
}

Then call setAdListener to set a listener to listen for the life cycle events of an interstitial ad and implement the callback events

public class InterstitialTest : MonoBehaviour
{
...
private AndroidJavaObject mHwInterstitialAd;
...
private void handleRequestAd()
{
...
// Set the ad listener.
AdStatusListener adStatusListener = new AdStatusListener();
adListener.mOnAdLoaded += onAdLoadSuccess;
adListener.mOnAdFailed += onAdLoadFail;
mHwInterstitialAd.Call("setAdListener", adStatusListener);
...
}

private void onAdLoadSuccess(object sender, EventArgs args)
{
...
}

private void onAdLoadFail(object sender, AdLoadErrorCodeEventArgs args)
{
...
}
}

Creating a sample scene to load and show Interstitial Ad

In Scenes of Unity Editor, create a RequestAd button for loading an ad and create a ShowAd button for displaying an ad.

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/043/403/889/5190081000043403889.20200903085553.62960173543807314113831216431422:50510915093939:2800:37E42330EF9AB8F1D2B2E024798DFC3258854EFAA50F494DBA636A8EED45BAC1.png"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/043/403/889/5190081000043403889.20200903085553.62960173543807314113831216431422:50510915093939:2800:37E42330EF9AB8F1D2B2E024798DFC3258854EFAA50F494DBA636A8EED45BAC1.png[/IMG]

Define a click event for each button to specify the processing after each button is clicked.

public class InterstitialTest : MonoBehaviour
{
...
private GameObject mLoadButton;
private AndroidJavaObject mHwInterstitialAd;
...
void Start()
{
mLoadButton = GameObject.Find("RequestAd");
mLoadButton.GetComponent<Button>().onClick.AddListener(handleRequestAd);
mShowButton = GameObject.Find("ShowAd");
mShowButton.GetComponent<Button>().onClick.AddListener(handleShowAd);
}
...
private void handleRequestAd()
{
...
}
...
private void handleShowAd()
{
...
}
...
}

Loading an Interstitial Ad

Implement the loadAd method in your Android project's InterstitialAdProxy class as below

class InterstitialAdProxy(private val context: context) {

...
private val mInterstitialAd: InterstitialAd
...
fun loadAd(adRequest: AdParam?) {
mInterstitialAd.loadAd(adRequest)
}
...
}

Then call this method from Unity script as below

public class InterstitialTest : MonoBehaviour
{
...
private AndroidJavaObject mHwInterstitialAd;
...
private void handleRequestAd()
{
...
// Load an ad.
UnityHwAdRequest adRequest = new UnityHwAdRequest.Builder().build();
mHwInterstitialAd.Call("loadAd", adRequest.getAdRequestJavaObject());
...
}
...
}

The request parameter class UnityHwAdRequest also needs to be defined in the Unity project and the reflection class of the Ads SDK needs to be obtained to pass ad request parameters.

public class UnityHwAdRequest
{
...
public AndroidJavaObject getAdRequestJavaObject()
{
AndroidJavaObject adRequestBuilder = new AndroidJavaObject(Constant.AdRequestBuilderName);
foreach (string keyword in mKeyWords)
{
adRequestBuilder.Call<AndroidJavaObject>("addKeyword", keyword);
}
if (mTargetingContentUrl != null)
{
adRequestBuilder.Call<AndroidJavaObject>("setTargetingContentUrl", mTargetingContentUrl);
}
...
}
}

The following shows the AdRequestBuilderName setting.

public class Constant
{
...
public const string AdRequestBuilderName = "com.huawei.hms.ads.AdParam$Builder";
...
}

Displaying an Interstitial Ad

Implement isLoaded and show methods in your Android project's InterstitialAdProxy class as below

class InterstitialAdProxy(private val context: context) {

...
private val mInterstitialAd: InterstitialAd
...

val isLoaded: Boolean
get() = mInterstitialAd.isLoaded()

fun show() {
mInterstitialAd.show()
}
...
}

When the button for displaying an interstitial ad is clicked, handleShowAd is called to display the ad. In the handleShowAd method, the isLoaded method of the proxy object of the AndroidJavaObject type can be used to determine whether the ad loading is complete. If the returned value of the method is true, the ad loading is complete. In this case, the show method of the AndroidJavaObject object can be called to display the interstitial ad.

public class InterstitialTest : MonoBehaviour
{
...
private void handleShowAd()
{
if(mHwInterstitialAd != null && mHwInterstitialAd.Call<bool>("isLoaded"))
{
mHwInterstitialAd.Call("show");
}
else
{
...
}
}
}

Demo result

The ad display will be as follow

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/043/403/889/5190081000043403889.20200903092822.43188917464017010824587753561090:50510915093939:2800:5A398022B77966A5904CFCD5FD2C33C4BF9C1F2C88C0EEF8CDC116944CBA0EEC.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/043/403/889/5190081000043403889.20200903092822.43188917464017010824587753561090:50510915093939:2800:5A398022B77966A5904CFCD5FD2C33C4BF9C1F2C88C0EEF8CDC116944CBA0EEC.jpg[/IMG]

[IMG="https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/043/403/889/5190081000043403889.20200903092822.80809286291223495290123775248627:50510915093939:2800:0D1B6D99EDB0D35841678CFC89575E12CB910E6C3E147A8EE6EE457AAA9E8C45.jpg"]https://communityfile-dre.op.hicloud.com/FileServer/getFile/cmtybbs/043/403/889/5190081000043403889.20200903092822.80809286291223495290123775248627:50510915093939:2800:0D1B6D99EDB0D35841678CFC89575E12CB910E6C3E147A8EE6EE457AAA9E8C45.jpg[/IMG]

I will describe how to load and display a Rewarded Ad in the next post.