Implementing interstitial ads in Android

To display a full-screen interstitial ad using the UnityAds API:

  1. Initialize the SDK.
  2. Use the load API to load an ad for the specified Ad Unit.
  3. After the ad loads, you can then display it using the show API.

Interstitial ads example

In this example, you can invoke DisplayInterstitialAd from anywhere in your game to show an interstitial ad as long as Unity Ads has successfully initialized.

Note: Unity Ads requires access to the currently running Activity, so the following example uses getApplicationContext(). Because this might not be suitable for all implementations, some customization might be required (depending on the integration).

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.unity3d.ads.IUnityAdsLoadListener;
import com.unity3d.ads.IUnityAdsShowListener;
import com.unity3d.ads.UnityAdsShowOptions;
import com.unity3d.ads.example.R;

import com.unity3d.ads.IUnityAdsInitializationListener;
import com.unity3d.ads.UnityAds;

public class ShowInterstitialAd extends AppCompatActivity implements IUnityAdsInitializationListener  {

  private String unityGameID = "1234567";
  private Boolean testMode = true;
  private String adUnitId = "video";

  private IUnityAdsLoadListener loadListener = new IUnityAdsLoadListener() {
     @Override
     public void onUnityAdsAdLoaded(String placementId) {
        UnityAds.show((Activity)getApplicationContext(), adUnitId, new UnityAdsShowOptions(), showListener);
     }

     @Override
     public void onUnityAdsFailedToLoad(String placementId, UnityAds.UnityAdsLoadError error, String message) {
        Log.e("UnityAdsExample", "Unity Ads failed to load ad for " + placementId + " with error: [" + error + "] " + message);
     }
  };

  private IUnityAdsShowListener showListener = new IUnityAdsShowListener() {
     @Override
     public void onUnityAdsShowFailure(String placementId, UnityAds.UnityAdsShowError error, String message) {
        Log.e("UnityAdsExample", "Unity Ads failed to show ad for " + placementId + " with error: [" + error + "] " + message);
     }

     @Override
     public void onUnityAdsShowStart(String placementId) {
        Log.v("UnityAdsExample", "onUnityAdsShowStart: " + placementId);
     }

     @Override
     public void onUnityAdsShowClick(String placementId) {
        Log.v("UnityAdsExample", "onUnityAdsShowClick: " + placementId);
     }

     @Override
     public void onUnityAdsShowComplete(String placementId, UnityAds.UnityAdsShowCompletionState state) {
        Log.v("UnityAdsExample", "onUnityAdsShowComplete: " + placementId);
     }
  };

  @Override
  protected void onCreate (Bundle savedInstanceState) {
     super.onCreate (savedInstanceState);
     setContentView (R.layout.activity_main);
     // Initialize the SDK:
     UnityAds.initialize(getApplicationContext(), unityGameID, testMode, this);
  }

  @Override
  public void onInitializationComplete() {
     DisplayInterstitialAd();
  }

  @Override
  public void onInitializationFailed(UnityAds.UnityAdsInitializationError error, String message) {
     Log.e("UnityAdsExample", "Unity Ads initialization failed with error: [" + error + "] " + message);
  }

// Implement a function to load an interstitial ad. The ad will start to show after the ad has been loaded.
  public void DisplayInterstitialAd () {
     UnityAds.load(adUnitId, loadListener);
  }
}

Next steps: To improve your implementation, refer to the Implementing rewarded ads in Android documentation.