My admob account is linked to the app in play store, status "ready", AdMob app ID also added into ad settings in Unity, added in the real ad unit ID, test ad showing in Unity, waited a week...
Result: no real ads
My event is calling RequestAndLoadInterstitialAd() and ShowInterstitialAd(), which works in Unity with the test ad. I expected it to work once the test ad implemented successfully.
using UnityEngine.Events;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
using UnityEngine.UI;
using System;
using System.Collections.Generic;
public class GoogleAdMobController : MonoBehaviour
{
private AppOpenAd appOpenAd;
private BannerView bannerView;
private InterstitialAd interstitialAd;
private RewardedAd rewardedAd;
private RewardedInterstitialAd rewardedInterstitialAd;
private float deltaTime;
private bool isShowingAppOpenAd;
public UnityEvent OnAdLoadedEvent;
public UnityEvent OnAdFailedToLoadEvent;
public UnityEvent OnAdOpeningEvent;
public UnityEvent OnAdFailedToShowEvent;
public UnityEvent OnUserEarnedRewardEvent;
public UnityEvent OnAdClosedEvent;
public bool showFpsMeter = true;
public Text fpsMeter;
public Text statusText;
#region UNITY MONOBEHAVIOR METHODS
public void Start()
{
MobileAds.SetiOSAppPauseOnBackground(true);
List<String> deviceIds = new List<String>() { AdRequest.TestDeviceSimulator };
// Add some test device IDs (replace with your own device IDs).
#if UNITY_IPHONE
deviceIds.Add("b77a5c561934e089)--a185c62eba2497c95197140e5282b27a");
#elif UNITY_ANDROID
deviceIds.Add("b77a5c561934e089)--a185c62eba2497c95197140e5282b27a");
#endif
// Configure TagForChildDirectedTreatment and test device IDs.
RequestConfiguration requestConfiguration =
new RequestConfiguration.Builder()
.SetTagForChildDirectedTreatment(TagForChildDirectedTreatment.Unspecified)
.SetTestDeviceIds(deviceIds).build();
MobileAds.SetRequestConfiguration(requestConfiguration);
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(HandleInitCompleteAction);
}
private void HandleInitCompleteAction(InitializationStatus initstatus)
{
// Callbacks from GoogleMobileAds are not guaranteed to be called on
// main thread.
// In this example we use MobileAdsEventExecutor to schedule these calls on
// the next Update() loop.
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
statusText.text = "Initialization complete";
// RequestBannerAd();
});
}
private void Update()
{
if (showFpsMeter)
{
fpsMeter.gameObject.SetActive(true);
deltaTime = (Time.deltaTime - deltaTime) * 0.1f;
float fps = 1.0f / deltaTime;
fpsMeter.text = string.Format("{0:0.} fps", fps);
}
else
{
fpsMeter.gameObject.SetActive(false);
}
}
#endregion
#region HELPER METHODS
private AdRequest CreateAdRequest()
{
return new AdRequest.Builder()
.AddKeyword("unity-admob-sample")
.Build();
}
public void OnApplicationPause(bool paused)
{
// Display the app open ad when the app is foregrounded.
if (!paused)
{
ShowAppOpenAd();
}
}
#endregion
#region BANNER ADS
public void RequestBannerAd()
{
statusText.text = "Requesting Banner Ad.";
// These ad units are configured to always serve test ads.
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/6300978111";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/2934735716";
#else
string adUnitId = "unexpected_platform";
#endif
// Clean up banner before reusing
if (bannerView != null)
{
bannerView.Destroy();
}
// Create a 320x50 banner at top of the screen
bannerView = new BannerView(adUnitId, AdSize.SmartBanner, AdPosition.Top);
// Add Event Handlers
bannerView.OnAdLoaded = (sender, args) => OnAdLoadedEvent.Invoke();
bannerView.OnAdFailedToLoad = (sender, args) => OnAdFailedToLoadEvent.Invoke();
bannerView.OnAdOpening = (sender, args) => OnAdOpeningEvent.Invoke();
bannerView.OnAdClosed = (sender, args) => OnAdClosedEvent.Invoke();
// Load a banner ad
bannerView.LoadAd(CreateAdRequest());
}
public void DestroyBannerAd()
{
if (bannerView != null)
{
bannerView.Destroy();
}
}
#endregion
#region INTERSTITIAL ADS
public void RequestAndLoadInterstitialAd()
{
// statusText.text = "Requesting Interstitial Ad.";
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-2334240444886107/9358512518";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
string adUnitId = "unexpected_platform";
#endif
// Clean up interstitial before using it
if (interstitialAd != null)
{
interstitialAd.Destroy();
}
interstitialAd = new InterstitialAd(adUnitId);
// Add Event Handlers
interstitialAd.OnAdLoaded = (sender, args) => OnAdLoadedEvent.Invoke();
interstitialAd.OnAdFailedToLoad = (sender, args) => OnAdFailedToLoadEvent.Invoke();
interstitialAd.OnAdOpening = (sender, args) => OnAdOpeningEvent.Invoke();
interstitialAd.OnAdClosed = (sender, args) => OnAdClosedEvent.Invoke();
// Load an interstitial ad
interstitialAd.LoadAd(CreateAdRequest());
}
public void ShowInterstitialAd()
{
if (interstitialAd.IsLoaded())
{
interstitialAd.Show();
}
else
{
statusText.text = "Interstitial ad is not ready yet";
}
}
public void DestroyInterstitialAd()
{
if (interstitialAd != null)
{
interstitialAd.Destroy();
}
}
#endregion
#region REWARDED ADS
public void RequestAndLoadRewardedAd()
{
statusText.text = "Requesting Rewarded Ad.";
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
string adUnitId = "unexpected_platform";
#endif
// create new rewarded ad instance
rewardedAd = new RewardedAd(adUnitId);
// Add Event Handlers
rewardedAd.OnAdLoaded = (sender, args) => OnAdLoadedEvent.Invoke();
rewardedAd.OnAdFailedToLoad = (sender, args) => OnAdFailedToLoadEvent.Invoke();
rewardedAd.OnAdOpening = (sender, args) => OnAdOpeningEvent.Invoke();
rewardedAd.OnAdFailedToShow = (sender, args) => OnAdFailedToShowEvent.Invoke();
rewardedAd.OnAdClosed = (sender, args) => OnAdClosedEvent.Invoke();
rewardedAd.OnUserEarnedReward = (sender, args) => OnUserEarnedRewardEvent.Invoke();
// Create empty ad request
rewardedAd.LoadAd(CreateAdRequest());
}
public void ShowRewardedAd()
{
if (rewardedAd != null)
{
rewardedAd.Show();
}
else
{
statusText.text = "Rewarded ad is not ready yet.";
}
}
public void RequestAndLoadRewardedInterstitialAd()
{
statusText.text = "Requesting Rewarded Interstitial Ad.";
// These ad units are configured to always serve test ads.
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/5354046379";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/6978759866";
#else
string adUnitId = "unexpected_platform";
#endif
// Create an interstitial.
RewardedInterstitialAd.LoadAd(adUnitId, CreateAdRequest(), (rewardedInterstitialAd, error) =>
{
if (error != null)
{
MobileAdsEventExecutor.ExecuteInUpdate(() => {
statusText.text = "RewardedInterstitialAd load failed, error: " error;
});
return;
}
this.rewardedInterstitialAd = rewardedInterstitialAd;
MobileAdsEventExecutor.ExecuteInUpdate(() => {
statusText.text = "RewardedInterstitialAd loaded";
});
// Register for ad events.
this.rewardedInterstitialAd.OnAdDidPresentFullScreenContent = (sender, args) =>
{
MobileAdsEventExecutor.ExecuteInUpdate(() => {
statusText.text = "Rewarded Interstitial presented.";
});
};
this.rewardedInterstitialAd.OnAdDidDismissFullScreenContent = (sender, args) =>
{
MobileAdsEventExecutor.ExecuteInUpdate(() => {
statusText.text = "Rewarded Interstitial dismissed.";
});
this.rewardedInterstitialAd = null;
};
this.rewardedInterstitialAd.OnAdFailedToPresentFullScreenContent = (sender, args) =>
{
MobileAdsEventExecutor.ExecuteInUpdate(() => {
statusText.text = "Rewarded Interstitial failed to present.";
});
this.rewardedInterstitialAd = null;
};
});
}
public void ShowRewardedInterstitialAd()
{
if (rewardedInterstitialAd != null)
{
rewardedInterstitialAd.Show((reward) => {
MobileAdsEventExecutor.ExecuteInUpdate(() => {
statusText.text = "User Rewarded: " reward.Amount;
});
});
}
else
{
statusText.text = "Rewarded ad is not ready yet.";
}
}
#endregion
#region APPOPEN ADS
public void RequestAndLoadAppOpenAd()
{
statusText.text = "Requesting App Open Ad.";
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/3419835294";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/5662855259";
#else
string adUnitId = "unexpected_platform";
#endif
// create new app open ad instance
AppOpenAd.LoadAd(adUnitId, ScreenOrientation.Portrait, CreateAdRequest(), (appOpenAd, error) =>
{
if (error != null)
{
MobileAdsEventExecutor.ExecuteInUpdate(() => {
statusText.text = "AppOpenAd load failed, error: " error;
});
return;
}
MobileAdsEventExecutor.ExecuteInUpdate(() => {
statusText.text = "AppOpenAd loaded. Please background the app and return.";
});
this.appOpenAd = appOpenAd;
});
}
public void ShowAppOpenAd()
{
if (isShowingAppOpenAd)
{
return;
}
if (appOpenAd == null)
{
return;
}
// Register for ad events.
this.appOpenAd.OnAdDidDismissFullScreenContent = (sender, args) =>
{
isShowingAppOpenAd = false;
MobileAdsEventExecutor.ExecuteInUpdate(() => {
Debug.Log("AppOpenAd dismissed.");
if (this.appOpenAd != null)
{
this.appOpenAd.Destroy();
this.appOpenAd = null;
}
});
};
this.appOpenAd.OnAdFailedToPresentFullScreenContent = (sender, args) =>
{
isShowingAppOpenAd = false;
var msg = args.AdError.GetMessage();
MobileAdsEventExecutor.ExecuteInUpdate(() => {
statusText.text = "AppOpenAd present failed, error: " msg;
if (this.appOpenAd != null)
{
this.appOpenAd.Destroy();
this.appOpenAd = null;
}
});
};
this.appOpenAd.OnAdDidPresentFullScreenContent = (sender, args) =>
{
isShowingAppOpenAd = true;
MobileAdsEventExecutor.ExecuteInUpdate(() => {
Debug.Log("AppOpenAd presented.");
});
};
this.appOpenAd.OnAdDidRecordImpression = (sender, args) =>
{
MobileAdsEventExecutor.ExecuteInUpdate(() => {
Debug.Log("AppOpenAd recorded an impression.");
});
};
this.appOpenAd.OnPaidEvent = (sender, args) =>
{
string currencyCode = args.AdValue.CurrencyCode;
long adValue = args.AdValue.Value;
string suffix = "AppOpenAd received a paid event.";
MobileAdsEventExecutor.ExecuteInUpdate(() => {
string msg = string.Format("{0} (currency: {1}, value: {2}", suffix, currencyCode, adValue);
statusText.text = msg;
});
};
appOpenAd.Show();
}
#endregion
#region AD INSPECTOR
public void OpenAdInspector()
{
statusText.text = "Open Ad Inspector.";
MobileAds.OpenAdInspector((error) =>
{
if (error != null)
{
string errorMessage = error.GetMessage();
MobileAdsEventExecutor.ExecuteInUpdate(() => {
statusText.text = "Ad Inspector failed to open, error: " errorMessage;
});
}
else
{
MobileAdsEventExecutor.ExecuteInUpdate(() => {
statusText.text = "Ad Inspector closed.";
});
}
});
}
#endregion
}
CodePudding user response:
If your test device (You can add this from Admob) shows your ADs as expected you should be fine. After some requests, your game will start to show real ads. Admob does not give real ads immediately, it needs some request to show real ads. But as I say you have to be sure your test devices show ads correctly.
- And I also recommend check compatible admob SDK and Unity versions. Because they have bugs sometimes. You can check it in Admob GitHub page. You can check here:
https://github.com/googleads/googleads-mobile-unity/releases
CodePudding user response:
There were two things wrong. One, I was using my live adUnit ID not the test one. The second issue was I was calling the load and show methods at the same time which worked in Unity, but on Android it seems there needs to be more time for the ad to load.
What I did to get the test ads on mobile device:
Set up the test device like HTugsadK said https://support.google.com/admob/answer/9691433
Change the adUnitId in RequestAndLoadInterstitialAd() to the interstitial test ID from here https://developers.google.com/admob/unity/test-ads#add_your_test_device_programmatically For production this should be changed back to the real adUnitId.
Change the end of the RequestAndLoadInterstitialAd() method and Invoke the show method with a 5 second delay. Now in your event you only have to call this method.
public void RequestAndLoadInterstitialAd() { // statusText.text = "Requesting Interstitial Ad."; #if UNITY_EDITOR string adUnitId = "unused"; #elif UNITY_ANDROID string adUnitId = "ca-app-pub-3940256099942544/1033173712"; #elif UNITY_IPHONE string adUnitId = "ca-app-pub-3940256099942544/4411468910"; #else string adUnitId = "unexpected_platform"; #endif // Clean up interstitial before using it if (interstitialAd != null) { interstitialAd.Destroy(); } interstitialAd = new InterstitialAd(adUnitId); // Add Event Handlers interstitialAd.OnAdLoaded = (sender, args) => OnAdLoadedEvent.Invoke(); interstitialAd.OnAdFailedToLoad = (sender, args) => OnAdFailedToLoadEvent.Invoke(); interstitialAd.OnAdOpening = (sender, args) => OnAdOpeningEvent.Invoke(); interstitialAd.OnAdClosed = (sender, args) => OnAdClosedEvent.Invoke(); // Load an interstitial ad interstitialAd.LoadAd(CreateAdRequest()); Invoke("ShowInterstitialAd", 5); }