Hi, I’m new to AdMob and recently implemented a straightforward Ad Banner script to manage two types of ads: SmallBannerAd and BigBannerAd. Initially, everything works fine as all ads load correctly when the game starts. However, I encountered an issue – when I open any banner ad and return to the game, both ads disappear.
To address this problem i attempt to fix this issue i discover i have to destory then load ad after everytime i open banner ad and i have to same for other ad. i check on internet but i not find any solution. anyone known how to fix it
using UnityEngine;
using System.Collections;
using GoogleMobileAds.Api;
public class AdBannerManager : MonoBehaviour
{
BannerView Small_bannerView;
BannerView Big_bannerView;
private bool isBigBannerLoaded = false;
private bool isSmallBannerLoaded = false;
private string Small_Banner = "ca-app-pub-3940256099942544/6300978111";
private string Big_Banner = "ca-app-pub-3940256099942544/6300978111";
private int maxRetryAttempts = 5;
private float baseRetryDelay = 1f;
private float maxRetryDelay = 60f;
private Coroutine retryCoroutine;
public void Start()
{
MobileAds.Initialize((InitializationStatus initStatus) =>
{
});
PreloadBannerAds();
}
public void PreloadBannerAds()
{
if (GetComponent<AdManager>().AdsEnabled)
{
RetryAdLoading();
}
}
private void RetryAdLoading()
{
if (retryCoroutine == null)
{
retryCoroutine = StartCoroutine(RetryWithExponentialBackoff());
}
}
private IEnumerator RetryWithExponentialBackoff()
{
int retryAttempt = 0;
while (retryAttempt < maxRetryAttempts)
{
float delay = Mathf.Pow(2, retryAttempt) * baseRetryDelay;
delay = Mathf.Min(delay, maxRetryDelay);
Debug.Log($"Retrying ad loading in {delay} seconds...");
yield return new WaitForSeconds(delay);
if (IsInternetAvailable())
{
Debug.Log("Internet is available. Retrying ad request...");
// Load small banner ad
LoadSmallBannerAd();
// Load big banner ad
LoadBigBannerAd();
// Hide both banners initially
Small_bannerView?.Hide();
Big_bannerView?.Hide();
retryCoroutine = null;
yield break;
}
else
{
Debug.Log("Internet is still not available. Retrying...");
retryAttempt++;
}
}
Debug.Log("Reached maximum retry attempts. Stopping retries.");
retryCoroutine = null;
}
private bool IsInternetAvailable()
{
if (Application.internetReachability != NetworkReachability.NotReachable)
{
using (var webClient = new System.Net.WebClient())
{
try
{
using (webClient.OpenRead("http://clients3.google.com/generate_204"))
{
return true;
}
}
catch (System.Exception)
{
return false;
}
}
}
return false;
}
public void LoadSmallBannerAd()
{
// create an instance of a banner view first.
if (Small_bannerView == null)
{
CreateSmallBannerView();
ListenToAdEvents();
}
// create our request used to load the ad.
var adRequest = new AdRequest();
adRequest.Keywords.Add("unity-admob-sample"); //........................................ Remove on Production Build
// send the request to load the ad.
Debug.Log("Loading Small banner ad.");
Small_bannerView.LoadAd(adRequest);
}
public void CreateSmallBannerView()
{
Debug.Log("Creating Small banner view");
// If we already have a banner, destroy the old one.
if (Small_bannerView != null)
{
DestroySmallBannerView();
}
// Create a 320x50 banner at top of the screen
Small_bannerView = new BannerView(Small_Banner, AdSize.Banner, AdPosition.Bottom);
}
private void ListenToAdEvents()
{
// Raised when an ad is loaded into the banner view.
Small_bannerView.OnBannerAdLoaded += () =>
{
Debug.Log("Banner view loaded an ad with response : "
+ Small_bannerView.GetResponseInfo());
// This event is triggered when an ad is successfully loaded into the banner view.
isSmallBannerLoaded = true; // Set the loaded state to true when the ad is loaded
};
// Raised when an ad fails to load into the banner view.
Small_bannerView.OnBannerAdLoadFailed += (LoadAdError error) =>
{
Debug.LogError("Banner view failed to load an ad with error : "
+ error);
// This event is triggered when there's a failure to load an ad into the banner view.
isSmallBannerLoaded = false; // Set the loaded state to false when the ad is loaded
RetryAdLoading(); // Retry ad loading upon failure
};
// Raised when the ad is estimated to have earned money.
Small_bannerView.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(string.Format("Banner view paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
// This event is triggered when the ad is estimated to have earned money.
};
// Raised when an impression is recorded for an ad.
Small_bannerView.OnAdImpressionRecorded += () =>
{
Debug.Log("Banner view recorded an impression.");
// This event is triggered when an impression is recorded for an ad.
};
// Raised when a click is recorded for an ad.
Small_bannerView.OnAdClicked += () =>
{
Debug.Log("Banner view was clicked.");
};
// Raised when an ad opened full screen content.
Small_bannerView.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Banner view full screen content opened.");
// This event is triggered when full-screen content (like an interstitial ad) opens from the banner.
};
// Raised when the ad closed full screen content.
Small_bannerView.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Banner view full screen content closed.");
// This event is triggered when full-screen content (like an interstitial ad) closes and returns to the app.
};
}
public void DestroySmallBannerView()
{
if (Small_bannerView != null)
{
Small_bannerView.Destroy();
Small_bannerView = null;
isSmallBannerLoaded = false; // Set the loaded state to false when the ad is loaded
}
}
/// <summary>
/// Big Banner Ad .............................................................................................................
/// </summary>
public void LoadBigBannerAd()
{
// create an instance of a banner view first.
if (Big_bannerView == null)
{
CreateBigBannerView();
ListenToAdEvents2();
}
// create our request used to load the ad.
var adRequest = new AdRequest();
adRequest.Keywords.Add("unity-admob-sample"); //........................................ Remove on Production Build
// send the request to load the ad.
Debug.Log("Loading Big banner ad.");
Big_bannerView.LoadAd(adRequest);
}
public void CreateBigBannerView()
{
Debug.Log("Creating Big banner view");
// If we already have a banner, destroy the old one.
if (Big_bannerView != null)
{
DestroyBigBannerView();
}
// Create a 320x50 banner at top of the screen
Big_bannerView = new BannerView(Big_Banner, AdSize.MediumRectangle, AdPosition.Bottom);
}
private void ListenToAdEvents2()
{
// Raised when an ad is loaded into the banner view.
Big_bannerView.OnBannerAdLoaded += () =>
{
Debug.Log("Banner view loaded an ad with response : "
+ Big_bannerView.GetResponseInfo());
// This event is triggered when an ad is successfully loaded into the banner view.
isBigBannerLoaded = true; // Set the loaded state to true when the ad is loaded
};
// Raised when an ad fails to load into the banner view.
Big_bannerView.OnBannerAdLoadFailed += (LoadAdError error) =>
{
Debug.LogError("Banner view failed to load an ad with error : "
+ error);
// This event is triggered when there's a failure to load an ad into the banner view.
isBigBannerLoaded = false; // Set the loaded state to false when the ad is loaded
RetryAdLoading(); // Retry ad loading upon failure
};
// Raised when the ad is estimated to have earned money.
Big_bannerView.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(string.Format("Banner view paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
// This event is triggered when the ad is estimated to have earned money.
};
// Raised when an impression is recorded for an ad.
Big_bannerView.OnAdImpressionRecorded += () =>
{
Debug.Log("Banner view recorded an impression.");
// This event is triggered when an impression is recorded for an ad.
};
// Raised when a click is recorded for an ad.
Big_bannerView.OnAdClicked += () =>
{
Debug.Log("Banner view was clicked.");
};
// Raised when an ad opened full screen content.
Big_bannerView.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Banner view full screen content opened.");
// This event is triggered when full-screen content (like an interstitial ad) opens from the banner.
};
// Raised when the ad closed full screen content.
Big_bannerView.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Banner view full screen content closed.");
// This event is triggered when full-screen content (like an interstitial ad) closes and returns to the app.
};
}
public void DestroyBigBannerView()
{
if (Big_bannerView != null)
{
Big_bannerView.Destroy();
Big_bannerView = null;
isBigBannerLoaded = false; // Set the loaded state to false when the ad is loaded
}
}
public void InitializeSmallBannerAd()
{
if (Small_bannerView != null)
{
if (isSmallBannerLoaded) // Check if the ad is already loaded
{
Small_bannerView.Show();
}
else
{
Debug.Log("Small banner ad not loaded. Loading ad...");
LoadSmallBannerAd(); // Load the ad if it's not already loaded
Small_bannerView.Show();
}
}
}
public void InitializeBigBannerAd()
{
if (Big_bannerView != null)
{
if (isBigBannerLoaded) // Check if the ad is already loaded
{
Big_bannerView.Show();
}
else
{
Debug.Log("Big banner ad not loaded. Loading ad...");
LoadBigBannerAd(); // Load the ad if it's not already loaded
Big_bannerView.Show();
}
}
}
// Hide both banners
public void HideSmallBanner()
{
if (Small_bannerView != null)
Small_bannerView.Hide();
}
// Hide both banners
public void HideBigBanner()
{
if (Big_bannerView != null)
Big_bannerView.Hide();
}
}