Google AdMob Unity:只有测试广告有效(在 Unity 编辑器中)但没有真正的广告
Google AdMob Unity: Only test ads work (in Unity Editor) but no real ads
我的 admob 帐户已链接到 play store 中的应用程序,状态为“准备就绪”,AdMob 应用程序 ID 也已添加到 Unity 中的广告设置中,添加到真实广告单元 ID 中,测试广告在 Unity 中显示,等了一个星期...
结果:没有真正的广告
我的事件是调用 RequestAndLoadInterstitialAd() 和 ShowInterstitialAd(),它们在 Unity 中与测试广告一起使用。我希望它能在测试广告成功实施后发挥作用。
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
}
如果您的测试设备(您可以从 Admob 添加)按预期显示您的广告,您应该没问题。经过一些请求后,您的游戏将开始显示真实广告。 Admob 不会立即提供真实广告,它需要一些请求才能显示真实广告。但正如我所说,您必须确保您的测试设备正确显示广告。
- 我还建议检查兼容的 admob SDK 和 Unity 版本。因为他们有时有错误。您可以在 Admob GitHub 页面中查看。
您可以在这里查看:
https://github.com/googleads/googleads-mobile-unity/releases
有两处错误。第一,我使用的是我的实时 adUnit ID 而不是测试 ID。第二个问题是我在 Unity 中同时调用了 load 和 show 方法,但在 Android 上似乎需要更多时间来加载广告。
我为在移动设备上获取测试广告所做的工作:
像HTugsadK说的那样设置测试设备https://support.google.com/admob/answer/9691433
从这里将 RequestAndLoadInterstitialAd() 中的 adUnitId 更改为插页式测试 ID https://developers.google.com/admob/unity/test-ads#add_your_test_device_programmatically
对于生产,这应该改回真正的 adUnitId。
更改 RequestAndLoadInterstitialAd() 方法的结尾并延迟 5 秒调用显示方法。现在在你的活动中你只需要调用这个方法。
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);
}
我的 admob 帐户已链接到 play store 中的应用程序,状态为“准备就绪”,AdMob 应用程序 ID 也已添加到 Unity 中的广告设置中,添加到真实广告单元 ID 中,测试广告在 Unity 中显示,等了一个星期...
结果:没有真正的广告
我的事件是调用 RequestAndLoadInterstitialAd() 和 ShowInterstitialAd(),它们在 Unity 中与测试广告一起使用。我希望它能在测试广告成功实施后发挥作用。
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
}
如果您的测试设备(您可以从 Admob 添加)按预期显示您的广告,您应该没问题。经过一些请求后,您的游戏将开始显示真实广告。 Admob 不会立即提供真实广告,它需要一些请求才能显示真实广告。但正如我所说,您必须确保您的测试设备正确显示广告。
- 我还建议检查兼容的 admob SDK 和 Unity 版本。因为他们有时有错误。您可以在 Admob GitHub 页面中查看。 您可以在这里查看:
https://github.com/googleads/googleads-mobile-unity/releases
有两处错误。第一,我使用的是我的实时 adUnit ID 而不是测试 ID。第二个问题是我在 Unity 中同时调用了 load 和 show 方法,但在 Android 上似乎需要更多时间来加载广告。
我为在移动设备上获取测试广告所做的工作:
像HTugsadK说的那样设置测试设备https://support.google.com/admob/answer/9691433
从这里将 RequestAndLoadInterstitialAd() 中的 adUnitId 更改为插页式测试 ID https://developers.google.com/admob/unity/test-ads#add_your_test_device_programmatically 对于生产,这应该改回真正的 adUnitId。
更改 RequestAndLoadInterstitialAd() 方法的结尾并延迟 5 秒调用显示方法。现在在你的活动中你只需要调用这个方法。
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); }