Unity and AdMob adapter

1380 views 2018-08-20 Ofer Garnett 0

Purpose

This document describes how to use YouAppi SDK on Unity with YouAppi AdMob adapter.

Setup Unity

Add AdMob mediation Unity package to your project (you can refer AdMob website for full integration instructions).

Android

  • Download YouAppi Android SDK from: youappi-sdk-android
  • In Unity project under the folder: Assets/Android add the following assets:
    • YouAppi’s SDK: youappi-sdk-android-moat.aar
    • Google Gson support: gson-2.8.0.jar (needed by YouAppi’s SDK).
    • YouAppi’s AdMob adapter: youappi-sdk-android-admob.aar

iOS

  • In Unity project under the folder: Assets/iOS add the following assets:
    • YouAppi’s SDK: YouAppiMoat.framework
    • YouAppi’s AdMob adapter: libAdmobYouAppiAdapter.a

Setup AdMob

  • Setup AdMob console with proper CUSTOM EVENT CLASS:
    • For Android:
      • Rewarded Video: com.youappi.sdk.mediation.admob.YouAppiRewardedVideo
      • Interstitial Video: com.youappi.sdk.mediation.admob.YouAppiInterstitialVideo
      • Interstitial Ad: com.youappi.sdk.mediation.admob.YouAppiInterstitialAd
    • For iOS:
      • Rewarded Video: YAAdMobRewardedVideoAdEvent
      • Interstitial Video: YAAdMobInterstitialVideoAdEvent
      • Interstitial Ad: YAAdMobInterstitialAdEvent
  • Setup AdMob console with proper CUSTOM EVENT DATA. For example:
{
  "accessToken": "821cfa77-3127-42b5-9e6b-0afcecf77c69",
  "adUnitId": "testRewardedVideoAdUnitId"
}

Here is how it looks on AdMob console:

Show Rewarded Video

Using AdMob mediation SDK create a rewarded video unit and use it to load a rewarded video and show it.

For example:

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
 
using GoogleMobileAds.Api;
 
public class Test : MonoBehaviour {
 
    private static readonly string AdUnitId = "< Your AdMob ad unit id >";
    private Text status_text;
 
    private RewardBasedVideoAd rewardBasedVideo;
 
    void Start () {
        Debug.Log("Initializing AdMob rewarded video");
 
        status_text = GameObject.Find("status_text").GetComponent<Text>();
 
        MobileAds.Initialize(AdUnitId);
 
        // Get singleton reward based video ad reference.
        this.rewardBasedVideo = RewardBasedVideoAd.Instance;
 
        // Called when an ad request has successfully loaded.
        rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
        // Called when an ad request failed to load.
        rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
    }
 
    public void HandleRewardBasedVideoLoaded(object sender , System.EventArgs args) {
        Debug.Log("Rewarded ad was loaded successfully");
        status_text.text = "Rewarded ad was loaded successfully";
    }
 
    public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args) {
        Debug.Log("Failed loading rewarded video due to: " + args.Message);
        status_text.text = "Failed loading rewarded video due to: " + args.Message;
    }
 
    public void loadRewardedVideo() {
        Debug.Log("Loading rewarded video");
        AdRequest request = new AdRequest.Builder().Build();
        rewardBasedVideo.LoadAd(request, AdUnitId);
    }
 
    public void showRewardedVideo() {
        if (rewardBasedVideo.IsLoaded()) {
            Debug.Log("Showing rewarded video");
            rewardBasedVideo.Show();
        }
        else {
            Debug.Log("Failed showing rewarded video. Ad unit is no loaded.");
        }
    }
}

Export project

Android

Simply export to Android.

iOS

After exporting to iOS project the following should be verified and setup:

  • Make sure to open the generated workspace file.
  • Set Enable Modules (C and Objective-C) to Yes.
  • In General Tab make sure Deployment Target is at least 8.0.
  • In General Tab add to Embedded Binaries: YouAppiMoat.framework.
  • In Build Settings set Always Embed Swift Standard Libraries  to Yes.
  • In Build Settings set Define Modules to Yes.