Hello, I need some help with my banner ad. It's initializing perfectly and working well. However, recently I tried to implement a feature where if the player clicks on the banner ad, I'd like it to refresh with a new request or a new ad
Here is my BannerAdCode and the point where I trigger its instantiation event, which I can share publicly. Unfortunately, destroying the banner ad leads to an instant crash. I attempted to address this issue by unsubscribing from the events, but it didn't resolve the problem. I'm willing to share any logs or folders if it would be helpful
void Start()
{
MobileAds.Initialize(initStatus =>
{
OnSceneLoaded(SceneManager.GetActiveScene(), LoadSceneMode.Single);
SceneManager.sceneLoaded += OnSceneLoaded;
});
}
private void OnSceneLoaded(Scene scene, LoadSceneMode sceneMode)
{
AdInitialized?.Invoke();
}
AdInitialized essentially switches scenes. I initiate this script on scene index 0 and maintain it until the index 1 scene (
GoogleAdMobInitialize is set as "don't destroy on Load").
public class BannerAdManager : MonoBehaviour
{
private string adUnitId = "this is my unit id";
BannerView _bannerView;
public static Action<string> BannerAdStatus;
public static Action<bool> BannerAdLoaded;
private void Awake()
{
GoogleAdMobInitialize.AdInitialized += OnAdMobInitialized;
}
private void OnDestroy()
{
GoogleAdMobInitialize.AdInitialized -= OnAdMobInitialized;
}
private void OnAdMobInitialized()
{
LoadAdBannerAd();
}
private void LoadAdBannerAd()
{
if (_bannerView != null) return;
_bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
var adRequest = new AdRequest();
Debug.Log("Loading banner ad.");
_bannerView.LoadAd(adRequest);
ListenToAdEvents();
}
/// <summary>
/// listen to events the banner view may raise.
/// </summary>
private void ListenToAdEvents()
{
// Raised when an ad is loaded into the banner view.
_bannerView.OnBannerAdLoaded += BannerLoadEvent;
// Raised when an ad fails to load into the banner view.
_bannerView.OnBannerAdLoadFailed += BannerAdRebuild;
// Raised when a click is recorded for an ad.
_bannerView.OnAdFullScreenContentClosed += BannerAdRebuild;
}
private void BannerLoadEvent()
{
Debug.Log("Banner view loaded an ad with response : "
+ _bannerView.GetResponseInfo());
BannerAdStatus("LOADED ");
BannerAdLoaded?.Invoke(true);
}
private void BannerAdRebuild(LoadAdError error)
{
DestroyBanner(LoadAdBannerAd);
}
private void BannerAdRebuild()
{
DestroyBanner(LoadAdBannerAd);
}
private void DestroyBanner(Action callback = null)
{
if (_bannerView != null)
{
_bannerView.OnBannerAdLoaded -= BannerLoadEvent;
_bannerView.OnBannerAdLoadFailed -= BannerAdRebuild;
_bannerView.OnAdFullScreenContentClosed -= BannerAdRebuild;
_bannerView.Destroy();
_bannerView = null;
}
callback?.Invoke();
}