Unity Interstitial Ads Integration: Step-by-Step Tutorial with Script

Learn how to integrate interstitial ads in your Unity game using the Google Mobile Ads SDK. This step-by-step guide includes a ready-to-use C# script
Unity Interstitial Ads Integration Tutorial

Unity Interstitial Ads Integration Tutorial

In this tutorial, you will learn how to integrate interstitial ads into your Unity project using the Google Mobile Ads SDK. Interstitial ads are full-screen ads displayed at natural transition points in your app, such as between levels of a game. Below is the complete C# script with detailed explanations and a "Copy Code" button for easy use.

Step-by-Step Explanation

  1. Initialize the SDK: The MobileAds.Initialize method initializes the Google Mobile Ads SDK.
  2. Load an Interstitial Ad: The LoadInterstitialAd method handles ad requests and ensures that ads are ready for display.
  3. Show the Ad: The ShowInterstitialAd method checks if an ad is ready before showing it to the user.
  4. Event Handlers: Ad lifecycle events like impressions, clicks, and fullscreen closures are logged and handled.
  5. Reload on Close: The RegisterReloadHandler ensures a new ad loads after the previous one closes.

Unity C# Script


using UnityEngine;
using GoogleMobileAds.Api;
using System;

public class InterstitialAdManager : MonoBehaviour
{
    public string InterstitialID = "";
    private InterstitialAd _interstitialAd;

    void Start()
    {
        MobileAds.Initialize((InitializationStatus initStatus) =>
        {
            LoadInterstitialAd();
        });

        Invoke("AutoShowAd", 5f);
    }

    public void LoadInterstitialAd()
    {
        if (_interstitialAd != null)
        {
            _interstitialAd.Destroy();
            _interstitialAd = null;
        }

        Debug.Log("Loading the interstitial ad.");
        var adRequest = new AdRequest();
        InterstitialAd.Load(InterstitialID, adRequest,
            (InterstitialAd ad, LoadAdError error) =>
            {
                if (error != null || ad == null)
                {
                    Debug.LogError("Interstitial ad failed to load: " + error);
                    return;
                }

                Debug.Log("Interstitial ad loaded.");
                _interstitialAd = ad;
                RegisterEventHandlers(_interstitialAd);
            });
    }

    public void ShowInterstitialAd()
    {
        if (_interstitialAd != null && _interstitialAd.CanShowAd())
        {
            Debug.Log("Showing interstitial ad.");
            _interstitialAd.Show();
        }
        else
        {
            Debug.LogError("Interstitial ad is not ready yet.");
        }
    }

    private void RegisterEventHandlers(InterstitialAd interstitialAd)
    {
        interstitialAd.OnAdPaid += (AdValue adValue) =>
        {
            Debug.Log($"Ad paid {adValue.Value} {adValue.CurrencyCode}.");
        };

        interstitialAd.OnAdImpressionRecorded += () =>
        {
            Debug.Log("Ad impression recorded.");
        };

        interstitialAd.OnAdClicked += () =>
        {
            Debug.Log("Ad clicked.");
        };

        interstitialAd.OnAdFullScreenContentOpened += () =>
        {
            Debug.Log("Ad opened fullscreen content.");
        };

        interstitialAd.OnAdFullScreenContentClosed += () =>
        {
            Debug.Log("Ad closed fullscreen content.");
            LoadInterstitialAd();
        };
    }
}
        

Copy the script above, replace InterstitialID with your Ad Unit ID, and attach the script to a GameObject in Unity to enable interstitial ads in your project.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.
NextGen Digital Welcome to WhatsApp chat
Howdy! How can we help you today?
Type here...