- Home /
Timer between ads displayed
Im using google admob and to make my users happy and not fed up with ads i want to space them out. I have a working code which i know works as in the console it says a message whenever one is supposed to display. Here is my code and i want to see how to add a 240 second timer between ads shown. Please help :).
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
public class AdmobManager : MonoBehaviour
{
private BannerView bannerView;
[SerializeField] private string appID = "";
[SerializeField] private string bannerID = "";
[SerializeField] private string regularAD = "";
private void Awake()
{
MobileAds.Initialize(appID);
}
public void OnclickShowBanner()
{
this.RequestBanner();
}
public void OnClickShowAd()
{
this.RequestRegularAd();
}
private void RequestRegularAd()
{
InterstitialAd AD = new InterstitialAd(regularAD);
AdRequest request = new AdRequest.Builder().Build();
AD.LoadAd(request);
}
private void RequestBanner()
{
bannerView = new BannerView(bannerID, AdSize.Banner, AdPosition.Bottom);
AdRequest request = new AdRequest.Builder().Build();
bannerView.LoadAd(request);
}
}
Comment
Best Answer
Answer by Hellium · Sep 26, 2019 at 02:25 PM
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
public class AdmobManager : MonoBehaviour
{
private BannerView bannerView;
[SerializeField] private string appID = "";
[SerializeField] private string bannerID = "";
[SerializeField] private string regularAD = "";
[SerializeField, Range(1, 900)] private int delayBetweenAds = 240;
private static float lastAdTime = Mathf.NegativeInfinity;
private void Awake()
{
MobileAds.Initialize(appID);
}
public void OnclickShowBanner()
{
this.RequestBanner();
}
public void OnClickShowAd()
{
if( (Time.time - lastAdTime) > (float) delayBetweenAds )
this.RequestRegularAd();
}
private void RequestRegularAd()
{
InterstitialAd AD = new InterstitialAd(regularAD);
AdRequest request = new AdRequest.Builder().Build();
AD.LoadAd(request);
lastAdTime = Time.time;
}
private void RequestBanner()
{
bannerView = new BannerView(bannerID, AdSize.Banner, AdPosition.Bottom);
AdRequest request = new AdRequest.Builder().Build();
bannerView.LoadAd(request);
}
}