Hello . I have a problem with Admob Ads , when I build the project and install my on my android device, ads not appearing . But in editor console i receive the following message .
In game , when i press button , i receive the following message "The add is not available for the moment " ,
from OnClick () . I try all my best before posting here , i already try to update GoogleMobileAds plugin .
Also i make a lot of test on my android device , but it’s seems i have only request , and the ads don’t show . Banner , interstitial , and rewarded
Thank you for you help .
using GoogleMobileAds.Api;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class RewardedVideoButton : MonoBehaviour
{
private const string ACTION_NAME = "rewarded_video";
private void Start()
{
Timer.Schedule(this, 0.1f, AddEvents);
}
private void AddEvents()
{
#if UNITY_ANDROID || UNITY_IOS
if (AdmobController.instance.rewardBasedVideo != null)
{
AdmobController.instance.rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}
#endif
}
public void OnClick()
{
if (IsAvailableToShow())
AdmobController.instance.ShowRewardBasedVideo();
else if (!IsAdAvailable())
Toast.instance.ShowMessage("Ad is not available at the moment");
else
{
int remainTime = (int)(GameConfig.instance.rewardedVideoPeriod - CUtils.GetActionDeltaTime(ACTION_NAME));
Toast.instance.ShowMessage("Please wait " + remainTime + " seconds for the next ad");
}
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
}
public bool IsAvailableToShow()
{
return IsActionAvailable() && IsAdAvailable();
}
private bool IsActionAvailable()
{
return CUtils.IsActionAvailable(ACTION_NAME, GameConfig.instance.rewardedVideoPeriod);
}
private bool IsAdAvailable()
{
if (AdmobController.instance.rewardBasedVideo == null) return false;
bool isLoaded = AdmobController.instance.rewardBasedVideo.IsLoaded();
return isLoaded;
}
private void OnDestroy()
{
#if UNITY_ANDROID || UNITY_IOS
if (AdmobController.instance.rewardBasedVideo != null)
{
AdmobController.instance.rewardBasedVideo.OnAdRewarded -= HandleRewardBasedVideoRewarded;
}
#endif
}
}
I will try to show a few code .
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameConfig : MonoBehaviour
{
[Header("Android")]
public string admobAndroidInterstitial;
public string admobAndroidRewardedVideo;
[Header("iOS")]
public string admobIOSInterstitial;
public string admobIOSRewardedVideo;
[Header("Other Settings")]
public int rewardedVideoPeriod;
public int rewardedVideoAmount;
public int adPeriod;
public static GameConfig instance;
private void Awake()
{
if (instance == null) instance = this;
}
}
using UnityEngine;
using System;
using GoogleMobileAds;
using GoogleMobileAds.Api;
[System.Serializable]
public class AdmobController : MonoBehaviour
{
private BannerView bannerView;
public InterstitialAd interstitial;
public RewardBasedVideoAd rewardBasedVideo;
public static AdmobController instance;
private void Awake()
{
if (instance == null) instance = this;
}
private void Start()
{
RequestBanner();
RequestInterstitial();
InitRewardedVideo();
RequestRewardBasedVideo();
}
private void InitRewardedVideo()
{
// Get singleton reward based video ad reference.
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
// RewardBasedVideoAd is a singleton, so handlers should only be registered once.
this.rewardBasedVideo.OnAdLoaded += this.HandleRewardBasedVideoLoaded;
this.rewardBasedVideo.OnAdFailedToLoad += this.HandleRewardBasedVideoFailedToLoad;
this.rewardBasedVideo.OnAdOpening += this.HandleRewardBasedVideoOpened;
this.rewardBasedVideo.OnAdStarted += this.HandleRewardBasedVideoStarted;
this.rewardBasedVideo.OnAdRewarded += this.HandleRewardBasedVideoRewarded;
this.rewardBasedVideo.OnAdClosed += this.HandleRewardBasedVideoClosed;
this.rewardBasedVideo.OnAdLeavingApplication += this.HandleRewardBasedVideoLeftApplication;
}
public void RequestBanner()
{
// These ad units are configured to always serve test ads.
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "";
#elif UNITY_IPHONE
string adUnitId = "");
#else
string adUnitId = "unexpected_platform";
#endif
// Create a 320x50 banner at the top of the screen.
this.bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Bottom);
// Register for ad events.
this.bannerView.OnAdLoaded += this.HandleAdLoaded;
this.bannerView.OnAdFailedToLoad += this.HandleAdFailedToLoad;
this.bannerView.OnAdOpening += this.HandleAdOpened;
this.bannerView.OnAdClosed += this.HandleAdClosed;
this.bannerView.OnAdLeavingApplication += this.HandleAdLeftApplication;
// Load a banner ad.
this.bannerView.LoadAd(this.CreateAdRequest());
}
public void RequestInterstitial()
{
// These ad units are configured to always serve test ads.
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = GameConfig.instance.admobAndroidInterstitial.Trim();
#elif UNITY_IPHONE
string adUnitId = GameConfig.instance.admobIOSInterstitial.Trim();
#else
string adUnitId = "unexpected_platform";
#endif
// Create an interstitial.
this.interstitial = new InterstitialAd(adUnitId);
// Register for ad events.
this.interstitial.OnAdLoaded += this.HandleInterstitialLoaded;
this.interstitial.OnAdFailedToLoad += this.HandleInterstitialFailedToLoad;
this.interstitial.OnAdOpening += this.HandleInterstitialOpened;
this.interstitial.OnAdClosed += this.HandleInterstitialClosed;
this.interstitial.OnAdLeavingApplication += this.HandleInterstitialLeftApplication;
// Load an interstitial ad.
this.interstitial.LoadAd(this.CreateAdRequest());
}
public void RequestRewardBasedVideo()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = GameConfig.instance.admobAndroidRewardedVideo.Trim();
#elif UNITY_IPHONE
string adUnitId = GameConfig.instance.admobIOSRewardedVideo.Trim();
#else
string adUnitId = "unexpected_platform";
#endif
this.rewardBasedVideo.LoadAd(this.CreateAdRequest(), adUnitId);
}
// Returns an ad request with custom ad targeting.
private AdRequest CreateAdRequest()
{
return new AdRequest.Builder()
.AddTestDevice(AdRequest.TestDeviceSimulator)
.AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
.AddKeyword("game")
.TagForChildDirectedTreatment(false)
.AddExtra("color_bg", "9B30FF")
.Build();
}
public void ShowInterstitial(InterstitialAd ad)
{
if (ad != null && ad.IsLoaded())
{
ad.Show();
}
}
public void ShowBanner()
{
if (bannerView != null)
{
bannerView.Show();
}
}
public void HideBanner()
{
if (bannerView != null)
{
bannerView.Hide();
}
}
public bool ShowInterstitial()
{
if (interstitial != null && interstitial.IsLoaded())
{
interstitial.Show();
return true;
}
return false;
}
public void ShowRewardBasedVideo()
{
if (this.rewardBasedVideo.IsLoaded())
{
this.rewardBasedVideo.Show();
}
else
{
MonoBehaviour.print("Reward based video ad is not ready yet");
}
}
#region Banner callback handlers
public void HandleAdLoaded(object sender, EventArgs args)
{
HideBanner();
print("HandleAdLoaded event received.");
}
public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("HandleFailedToReceiveAd event received with message: " + args.Message);
}
public void HandleAdOpened(object sender, EventArgs args)
{
print("HandleAdOpened event received");
}
public void HandleAdClosed(object sender, EventArgs args)
{
print("HandleAdClosed event received");
}
public void HandleAdLeftApplication(object sender, EventArgs args)
{
print("HandleAdLeftApplication event received");
}
#endregion
#region Interstitial callback handlers
public void HandleInterstitialLoaded(object sender, EventArgs args)
{
print("HandleInterstitialLoaded event received.");
}
public void HandleInterstitialFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
print("HandleInterstitialFailedToLoad event received with message: " + args.Message);
}
public void HandleInterstitialOpened(object sender, EventArgs args)
{
print("HandleInterstitialOpened event received");
}
public void HandleInterstitialClosed(object sender, EventArgs args)
{
print("HandleInterstitialClosed event received");
RequestInterstitial();
}
public void HandleInterstitialLeftApplication(object sender, EventArgs args)
{
print("HandleInterstitialLeftApplication event received");
}
#endregion
#region RewardBasedVideo callback handlers
public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print(
"HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
}
public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
RequestRewardBasedVideo();
MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
MonoBehaviour.print(
"HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " + type);
}
public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
#endregion
}