When ever I try to run ads on my project I get this error in the compiler : ![alt text][1]
[1]: /storage/temp/201680-screenshot-2022-11-10-183932.png
Any Idea on how I can fix it? Here is my code:
using System;
using System.Threading.Tasks;
using Unity.Services.Core;
using Unity.Services.Mediation;
using UnityEngine;
namespace Unity.Example
{
public class AdsInitializer : IDisposable
{
IInterstitialAd ad;
string adUnitId = "Interstitial_iOS";
string gameId = "5******";
public async Task InitServices()
{
try
{
InitializationOptions initializationOptions = new InitializationOptions();
initializationOptions.SetGameId(gameId);
await UnityServices.InitializeAsync(initializationOptions);
InitializationComplete();
}
catch (Exception e)
{
InitializationFailed(e);
}
}
public void SetupAd()
{
//Create
ad = MediationService.Instance.CreateInterstitialAd(adUnitId);
//Subscribe to events
ad.OnClosed += AdClosed;
ad.OnClicked += AdClicked;
ad.OnLoaded += AdLoaded;
ad.OnFailedLoad += AdFailedLoad;
// Impression Event
MediationService.Instance.ImpressionEventPublisher.OnImpression += ImpressionEvent;
}
public void Dispose() => ad?.Dispose();
public async void ShowAd()
{
if (ad.AdState == AdState.Loaded)
{
try
{
InterstitialAdShowOptions showOptions = new InterstitialAdShowOptions();
showOptions.AutoReload = true;
await ad.ShowAsync(showOptions);
AdShown();
}
catch (ShowFailedException e)
{
AdFailedShow(e);
}
}
}
void InitializationComplete()
{
SetupAd();
LoadAd();
}
async Task 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");
}
void AdFailedLoad(object sender, LoadErrorEventArgs e)
{
Debug.Log("Failed to load ad");
Debug.Log(e.Message);
}
void AdShown()
{
Debug.Log("Ad shown!");
}
void AdClosed(object sender, EventArgs e)
{
Debug.Log("Ad has closed");
// 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(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);
}
}
},I am using mediation ads and when I try copy code from the ads code generator I get this error. ![alt text][1]
[1]: /storage/temp/201679-screenshot-2022-11-10-183932.png
Any clue what I can do about it?
Here is my code :
using System;
using System.Threading.Tasks;
using Unity.Services.Core;
using Unity.Services.Mediation;
using UnityEngine;
namespace Unity.Example
{
public class AdsInitializer : IDisposable
{
IInterstitialAd ad;
string adUnitId = "Interstitial_iOS";
string gameId = "5******";
public async Task InitServices()
{
try
{
InitializationOptions initializationOptions = new InitializationOptions();
initializationOptions.SetGameId(gameId);
await UnityServices.InitializeAsync(initializationOptions);
InitializationComplete();
}
catch (Exception e)
{
InitializationFailed(e);
}
}
public void SetupAd()
{
//Create
ad = MediationService.Instance.CreateInterstitialAd(adUnitId);
//Subscribe to events
ad.OnClosed += AdClosed;
ad.OnClicked += AdClicked;
ad.OnLoaded += AdLoaded;
ad.OnFailedLoad += AdFailedLoad;
// Impression Event
MediationService.Instance.ImpressionEventPublisher.OnImpression += ImpressionEvent;
}
public void Dispose() => ad?.Dispose();
public async void ShowAd()
{
if (ad.AdState == AdState.Loaded)
{
try
{
InterstitialAdShowOptions showOptions = new InterstitialAdShowOptions();
showOptions.AutoReload = true;
await ad.ShowAsync(showOptions);
AdShown();
}
catch (ShowFailedException e)
{
AdFailedShow(e);
}
}
}
void InitializationComplete()
{
SetupAd();
LoadAd();
}
async Task 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");
}
void AdFailedLoad(object sender, LoadErrorEventArgs e)
{
Debug.Log("Failed to load ad");
Debug.Log(e.Message);
}
void AdShown()
{
Debug.Log("Ad shown!");
}
void AdClosed(object sender, EventArgs e)
{
Debug.Log("Ad has closed");
// 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(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);
}
}
}
↧