Hello,
I have implemented Mediation(Packages installed : “Ads Mediation” V1.0.0 and “Advertissement with Mediation” V1.0.5) for Ads in my game(Android).
But I have a problem: It works in the editor but not in the game build.
My app ID appears correctly in the Unity Mediation settings. Everything is ok in Unity DashBoard
And in the monetization tab, I see that there are requests but no impressions.
I use the Unity Mediation Code generator and made some little modification :
Banner Ad Script
using System;
using System.Threading.Tasks;
using Unity.Services.Core;
using Unity.Services.Mediation;
using UnityEngine;
public class BannerAdDisplay : MonoBehaviour, IDisposable
{
IBannerAd ad;
string adUnitId = "Banner_Android";
string gameId = "4493651";
public void Start()
{
//LetBeginBanner();
}
public void LetBeginBanner()
{
InitServices();
}
public async Task InitServices()
{
Debug.Log("Test initservice");
try
{
InitializationOptions initializationOptions = new InitializationOptions();
initializationOptions.SetGameId(gameId);
await UnityServices.InitializeAsync(initializationOptions);
InitializationComplete();
}
catch (Exception e)
{
InitializationFailed(e);
}
}
public void SetupAd()
{
Debug.Log("Test SetupAd");
//Create
ad = MediationService.Instance.CreateBannerAd(
adUnitId,
BannerAdPredefinedSize.Banner.ToBannerAdSize(),
BannerAdAnchor.BottomCenter,
Vector2.zero);
//Subscribe to events
ad.OnRefreshed += AdRefreshed;
ad.OnClicked += AdClicked;
ad.OnLoaded += AdLoaded;
ad.OnFailedLoad += AdFailedLoad;
// Impression Event
MediationService.Instance.ImpressionEventPublisher.OnImpression += ImpressionEvent;
}
public void Dispose() => ad?.Dispose();
void InitializationComplete()
{
Debug.Log("Test init complete");
SetupAd();
LoadAd();
}
async Task LoadAd()
{
Debug.Log("Test LoadAd");
try
{
await ad.LoadAsync();
}
catch (LoadFailedException)
{
// We will handle the failure in the OnFailedLoad callback
}
}
void InitializationFailed(Exception e)
{
Debug.Log("Test init failed");
Debug.Log("Initialization Failed: " + e.Message);
}
void AdLoaded(object sender, EventArgs e)
{
Debug.Log("Test AdLoaded");
Debug.Log("Ad loaded");
}
void AdFailedLoad(object sender, LoadErrorEventArgs e)
{
Debug.Log("Test AdFailedLoad");
Debug.Log("Failed to load ad");
Debug.Log(e.Message);
}
void AdRefreshed(object sender, LoadErrorEventArgs e)
{
Debug.Log("Refreshed ad");
//Debug.Log(e.Message);
}
void AdClicked(object sender, EventArgs e)
{
Debug.Log("Ad has been clicked");
// Execute logic after an ad has been clicked.
}
void ImpressionEvent(object sender, ImpressionEventArgs args)
{
var impressionData = args.ImpressionData != null ? JsonUtility.ToJson(args.ImpressionData, true) : "null";
Debug.Log("Impression event from ad unit id " + args.AdUnitId + " " + impressionData);
}
}
Reward Ad Script
using System;
using System.Threading.Tasks;
using Unity.Services.Core;
using Unity.Services.Mediation;
using UnityEngine;
public class AdDisplay : MonoBehaviour, IDisposable
{
IRewardedAd ad;
string adUnitId = "supportAd";
string gameId = "4493651";
int oneMoreLife;
bool haveBeenSeen = false;
//public GameObject panel;
public void LetsBegin()
{
InitServices();
haveBeenSeen = false;
}
public async Task InitServices()
{
Debug.Log("Test InitService !");
try
{
InitializationOptions initializationOptions = new InitializationOptions();
initializationOptions.SetGameId(gameId);
await UnityServices.InitializeAsync(initializationOptions);
InitializationComplete();
}
catch (Exception e)
{
InitializationFailed(e);
}
}
public void SetupAd()
{
Debug.Log("Test Setup !");
//Create
ad = MediationService.Instance.CreateRewardedAd(adUnitId);
//Subscribe to events
ad.OnClosed += AdClosed;
ad.OnClicked += AdClicked;
ad.OnLoaded += AdLoaded;
ad.OnFailedLoad += AdFailedLoad;
ad.OnUserRewarded += UserRewarded;
// Impression Event
MediationService.Instance.ImpressionEventPublisher.OnImpression += ImpressionEvent;
}
public void Dispose() => ad?.Dispose();
public async void ShowAd()
{
Debug.Log("Test ShowAd !");
if (ad.AdState == AdState.Loaded)
{
try
{
RewardedAdShowOptions showOptions = new RewardedAdShowOptions();
showOptions.AutoReload = true;
await ad.ShowAsync(showOptions);
AdShown();
}
catch (ShowFailedException e)
{
AdFailedShow(e);
}
}
}
public void InitializationComplete()
{
Debug.Log("Test InitilizationComplete !");
SetupAd();
LoadAd();
}
async Task LoadAd()
{
Debug.Log("Test LoadAd !");
try
{
await ad.LoadAsync();
}
catch (LoadFailedException)
{
// We will handle the failure in the OnFailedLoad callback
}
}
void InitializationFailed(Exception e)
{
Debug.Log("Initialization Failed: " + e.Message);
}
void AdLoaded(object sender, EventArgs e)
{
Debug.Log("Ad loaded");
if(!haveBeenSeen)
ShowAd();
}
void AdFailedLoad(object sender, LoadErrorEventArgs e)
{
Debug.Log("Failed to load ad");
Debug.Log(e.Message);
}
public void AdShown()
{
Debug.Log("Ad shown!");
int countNumerOfPart = 0;
PlayerPrefs.SetInt("countNumerOfPart", countNumerOfPart);
oneMoreLife = 1;
PlayerPrefs.SetInt("oneMoreLife", oneMoreLife);
// Give coins etc.
//if (panel != null)
//{
//panel.SetActive(false);
//}
//adStarted = false;
Time.timeScale = 1;
}
void AdClosed(object sender, EventArgs e)
{
Debug.Log("Ad has closed");
haveBeenSeen = true;
// Execute logic after an ad has been closed.
}
void AdClicked(object sender, EventArgs e)
{
Debug.Log("Ad has been clicked");
// Execute logic after an ad has been clicked.
}
void AdFailedShow(ShowFailedException e)
{
Debug.Log("Test AdFailedShow !");
Debug.Log(e.Message);
}
void ImpressionEvent(object sender, ImpressionEventArgs args)
{
var impressionData = args.ImpressionData != null ? JsonUtility.ToJson(args.ImpressionData, true) : "null";
Debug.Log("Impression event from ad unit id " + args.AdUnitId + " " + impressionData);
}
void UserRewarded(object sender, RewardEventArgs e)
{
Debug.Log($"Received reward: type:{e.Type}; amount:{e.Amount}");
}
}
The function “LetsBegin()” is called when player press a button to obtain reward
EDIT : and Here it is the logCat :
Error logger Initialization has failed due to: Request to https://mediation-instantiation.prd.mz.internal.unity3d.com/v1/initialize failed due to java.io.IOException: Instantiation Service initialization request failed with http status code 404 and server response: game init not found for game id: 4493651
I have a second question, in editor when, on my menu scene the banner ad is displayed but then I click on button to load another scene but the banner ad is still dsiplayed. I try the function Dispose to make it disappear but it’s not working
Could you please help me to know why ads are not displaying in build and how to make the banner ad disappear in another scene than the Menu one
Valentin Honoré



