I’m calling LoadBanner function from another script when the ads are initialized. Should i call that function later again to load another (new) banner ad. I think i shoud call that function when the banner is clicked, hided or shown, but i’m not sure. I’m not shure when the “OnBannerShown” function is called. When it’s shown in the beginning or competly shown, so i can call LoadBanner to load another ad.
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Advertisements;
using System.Collections;
public class BannerAd : MonoBehaviour
{
[SerializeField] BannerPosition _bannerPosition = BannerPosition.BOTTOM_CENTER;
[SerializeField] string _androidAdUnitId = "Banner_Android";
[SerializeField] string _iOSAdUnitId = "Banner_iOS";
string _adUnitId = null; // This will remain null for unsupported platforms.
void Start()
{
// Get the Ad Unit ID for the current platform:
#if UNITY_IOS
_adUnitId = _iOSAdUnitId;
#elif UNITY_ANDROID
_adUnitId = _androidAdUnitId;
#endif
// Set the banner position:
Advertisement.Banner.SetPosition(_bannerPosition);
}
// Implement a method to call when the Load Banner button is clicked:
public void LoadBanner()
{
// Set up options to notify the SDK of load events:
BannerLoadOptions options = new BannerLoadOptions
{
loadCallback = OnBannerLoaded,
errorCallback = OnBannerError
};
// Load the Ad Unit with banner content:
Advertisement.Banner.Load(_adUnitId, options);
}
// Implement code to execute when the loadCallback event triggers:
void OnBannerLoaded()
{
Debug.Log("Banner loaded");
ShowBannerAd();
}
// Implement code to execute when the load errorCallback event triggers:
void OnBannerError(string message)
{
Debug.Log($"Banner Error: {message}");
// Optionally execute additional code, such as attempting to load another ad.
LoadBanner();
}
// Implement a method to call when the Show Banner button is clicked:
void ShowBannerAd()
{
// Set up options to notify the SDK of show events:
BannerOptions options = new BannerOptions
{
clickCallback = OnBannerClicked,
hideCallback = OnBannerHidden,
showCallback = OnBannerShown
};
// Show the loaded Banner Ad Unit:
Advertisement.Banner.Show(_adUnitId, options);
}
// Implement a method to call when the Hide Banner button is clicked:
void HideBannerAd()
{
// Hide the banner:
Advertisement.Banner.Hide();
}
void OnBannerClicked() { }
void OnBannerShown() { }
void OnBannerHidden() { }
void OnDestroy()
{
}
}