Мне нужно звонить каждый раз, когда мне нужно показать объявление, поэтому я сделал это:
public class AdMobManager : MonoBehaviour
{
public static AdMobManager instance;//to have the right to call this class functions and variables without the need to put static to each one from another class
[SerializeField]
private string appID,interstitalAdID;
private InterstitialAd interstitalAd;
public bool isAdOpened,isAdClosed;
// Start is called before the first frame update
void Start()
{
instance = this;
MobileAds.Initialize(appID);
RequestInterstitialAd();//load an ad from the first time game is opened
}
// Update is called once per frame
void Update()
{
}
public void RequestInterstitialAd()
{
if(interstitalAd != null)//remove the old interstital ad showed before to create a new one
{
interstitalAd.Destroy();
}
interstitalAd = new InterstitialAd(interstitalAdID);
AdRequest adRequest = new AdRequest.Builder().Build();
interstitalAd.LoadAd(adRequest);
interstitalAd.OnAdLoaded += HandleOnAdLoaded;
interstitalAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
interstitalAd.OnAdOpening += HandleOnAdOpened;
interstitalAd.OnAdClosed += HandleOnAdClosed;
interstitalAd.OnAdLeavingApplication += HandleOnAdLeavingApplication;
Debug.Log("Ads Request Created");//used to test
}
public void DisplayInterstitialAd()
{
if (interstitalAd.IsLoaded())
{
interstitalAd.Show();
}
}
public void HandleOnAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLoaded event received");
}
public void HandleOnAdFailedToLoad(object sender,AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleFailedToReciveAd event received with message: " + args.Message);
}
public void HandleOnAdOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdOpened event received");
isAdOpened = true;
}
public void HandleOnAdClosed(object sender,EventArgs args)
{
MonoBehaviour.print("HandleAdClosed event received");
isAdClosed = true;
isAdOpened = false;//initialise
RequestInterstitialAd();//everytime we show an ad load another one when the first one is closed
}
public void HandleOnAdLeavingApplication(object sender,EventArgs args)
{
MonoBehaviour.print("HandleAdLeavingApplication event recived");
}
}