unity interstitial ads tutorial

Unity Interstitial Ads tutorial

Unity Interstitial Ads tutorial :- Integrating interstitial ads into your Unity game using Unity Ads is straightforward and involves a few key steps. Begin by setting up your Unity Ads account and configuring your game to enable ads. In your Unity project, import the Unity Ads SDK from the Unity Asset Store and initialize it in your startup script. Next, create and load interstitial ads using the SDK’s API, specifying your ad unit ID. Finally, implement logic to display these ads at appropriate moments in your game, such as during level transitions or app pauses. Make sure to handle ad callbacks to manage loading errors and user interactions effectively.

UNITY INTERSTITIAL ADS SCRIPT

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using UnityEngine.Advertisements;

public class UnityInterstitial : MonoBehaviour, IUnityAdsInitializationListener, IUnityAdsLoadListener, IUnityAdsShowListener
{

    public string gameID = "";
    public string interstitialID = "";

    [SerializeField] bool testMode = true;

    public static UnityInterstitial instance;


    // Start is called before the first frame update
    void Start()
    {

        InitializeAds();
        LoadInterstitialAd();

    }

    void Awake()
    {
        instance = this;

    }

    public void InitializeAds()
    {

        if (!Advertisement.isInitialized && Advertisement.isSupported)
        {
            Advertisement.Initialize(gameID, testMode, this);
        }

    }

    public void OnInitializationComplete()
    {
        Debug.Log("Unity Ads initialization complete.");
    }

    public void OnInitializationFailed(UnityAdsInitializationError error, string message)
    {
        Debug.Log($"Unity Ads Initialization Failed: {error.ToString()} - {message}");
    }

    public void LoadInterstitialAd()
    {
        // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script).
        Advertisement.Load(interstitialID, this);
    }


    public void ShowInterstitialAd()
    {
        // Note that if the ad content wasn't previously loaded, this method will fail
        Advertisement.Show(interstitialID, this);
    }

    // Implement Load Listener and Show Listener interface methods: 
    public void OnUnityAdsAdLoaded(string adUnitId)
    {
        // Optionally execute code if the Ad Unit successfully loads content.
    }

    public void OnUnityAdsFailedToLoad(string _adUnitId, UnityAdsLoadError error, string message)
    {
        Debug.Log($"Error loading Ad Unit: {_adUnitId} - {error.ToString()} - {message}");
        // Optionally execute code if the Ad Unit fails to load, such as attempting to try again.
        LoadInterstitialAd();
    }

    public void OnUnityAdsShowFailure(string _adUnitId, UnityAdsShowError error, string message)
    {
        Debug.Log($"Error showing Ad Unit {_adUnitId}: {error.ToString()} - {message}");
        // Optionally execute code if the Ad Unit fails to show, such as loading another ad.
        LoadInterstitialAd();
    }

    public void OnUnityAdsShowStart(string _adUnitId) { }
    public void OnUnityAdsShowClick(string _adUnitId) { }
    public void OnUnityAdsShowComplete(string _adUnitId, UnityAdsShowCompletionState showCompletionState) { }

}

***CHECK FULL VIDEO TUTORIAL***

Visit www.UnitySourceCode.store

Leave a Reply

Shopping cart

0
image/svg+xml

No products in the cart.

Continue Shopping