I’m currently trying to implement interstitial ads for android. I’m using the Advertisement Legacy package and version is 4.4.2.
Here is my code:
using System;
using System.Collections;
using System.Collections.Generic;
using Cinemachine;
using UnityEditor;
using UnityEngine.Advertisements;
using UnityEngine;
public class PlayerController : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
{
...
// ad variables
public string gameIdAndroid = "5367732";
public string gameIdIOS = "5367733";
public string adUnitIdAndroid = "Interstitial_Android";
public string adUnitIdIOS = "Interstitial_iOS";
public string adUnitId;
public bool adStarted;
private bool testMode = true;
private void Start()
{
...
Advertisement.Initialize(gameIdAndroid, testMode);
adUnitId = adUnitIdAndroid;
}
...
public void KillPlayer()
{
...
if (deaths % 2 == 0)
{
canMove = false;
PlayAd();
}
}
...
void PlayAd()
{
if (Advertisement.isInitialized && !adStarted)
{
Debug.Log("ad loading");
Advertisement.Load(adUnitId);
}
}
public void OnUnityAdsShowComplete(string adUnitIdc, UnityAdsShowCompletionState showCompletionState)
{
Debug.Log("ad completed");
if (showCompletionState == UnityAdsShowCompletionState.COMPLETED)
{
canMove = true;
adStarted = false;
Debug.Log("ad completed");
}
else if (showCompletionState == UnityAdsShowCompletionState.SKIPPED)
{
canMove = true;
adStarted = false;
Debug.Log("ad skipped");
}
}
public void OnUnityAdsFailedToLoad(string _adUnitId, UnityAdsLoadError error, string message)
{
Debug.Log($"Error loading Ad Unit: {_adUnitId} - {error.ToString()} - {message}");
adStarted = false;
canMove = true;
// Optionally execute code if the Ad Unit fails to load, such as attempting to try again.
}
public void OnUnityAdsShowFailure(string _adUnitId, UnityAdsShowError error, string message)
{
Debug.Log($"Error showing Ad Unit {_adUnitId}: {error.ToString()} - {message}");
adStarted = false;
canMove = true;
// Optionally execute code if the Ad Unit fails to show, such as loading another ad.
}
public void OnUnityAdsAdLoaded(string adId)
{
Debug.Log("ad loaded");
Advertisement.Show(adUnitId);
adStarted = true;
Debug.Log("playing ad");
}
public void OnUnityAdsShowStart(string adId)
{
}
public void OnUnityAdsShowClick(string adId)
{
}
}
In the editor, “ad loaded” is printed, and the errors “loadListener is null, you will not receive any callbacks” and “initializationListener is null, you will not receive any callbacks” are thrown. in the android build, “ad loaded” is NOT printed, and both errors again are thrown. In both cases no test ads are played.
Any help is appreciated!