Unity AdMob 广告不显示
Unity AdMob ads not displayed
我正在 Unity 5.5 上制作游戏,我已经按照 Google's Official AdMob Getting Started Guide 将 AdMob 集成到我的游戏中(目前仅 iOS,但 Android 也将在我得到这个工作)。
- 我已经在 AdMob 控制台设置了我的游戏并制作了一个基于奖励的视频广告。
- 我已将
GoogleMobileAds.framework
添加到我在 Xcode 中构建的项目中。
- 我已经确定我也在链接新添加的框架。
- 下载了Unity包,并根据需要集成到我的项目中。
- 我已经连接到事件处理程序:我的广告加载没有问题,没有失败。
当我尝试在 iOS 上显示广告时,ad.IsLoaded();
returns false 即使它刚刚加载(我的 OnAdLoaded
广告事件正在触发) .在 Unity 编辑器中,我看到以正确的顺序调用预期的虚拟方法(尽管没有显示任何内容,我认为这是 Unity 自己的编辑器的预期行为)。
这是我加载广告的代码(当然,我的发布商和广告单元 ID 已编辑):
public class AdManager : MonoBehaviour {
static RewardBasedVideoAd ad;
static UIManager uiManager;
#if UNITY_ANDROID
static string adUnitId = "ca-app-pub-XXX/YYY";
#elif UNITY_IPHONE
static string adUnitId = "ca-app-pub-XXX/YYY";
#else
static string adUnitId = "unexpected_platform";
#endif
static int headstartPoints;
// Use this for initialization
void Start () {
uiManager = GameObject.Find("Scripts").GetComponent<UIManager>();
ad = RewardBasedVideoAd.Instance;
ad.OnAdRewarded += Ad_OnAdRewarded;
ad.OnAdClosed += Ad_OnAdClosed;
ad.OnAdFailedToLoad += Ad_OnAdFailedToLoad;
ad.OnAdLoaded += Ad_OnAdLoaded;
headstartPoints = 0;
RequestNewAd();
}
void Ad_OnAdFailedToLoad (object sender, AdFailedToLoadEventArgs e)
{
Debug.Log("Ad failed to load.");
}
void Ad_OnAdLoaded (object sender, System.EventArgs e)
{
Debug.Log("Ad loaded.");
}
void Ad_OnAdClosed (object sender, System.EventArgs e)
{
Debug.Log("Ad was closed, proceeding to game without rewards...");
}
void Ad_OnAdRewarded (object sender, Reward e)
{
Debug.Log("Ad rewarded.");
headstartPoints = (int)e.Amount;
}
public static int GetAndConsumeRewards(){
int points = headstartPoints;
headstartPoints = 0;
return points;
}
public static void RequestNewAd(){
Debug.Log("Requested new ad.");
AdRequest request = new AdRequest.Builder().Build();
ad.LoadAd(request, adUnitId);
}
public static void DisplayAdOrProceed(){
if(ad.IsLoaded()){
ad.Show();
Debug.Log("Ad was loaded, displaying ad...");
}else{
Debug.Log("Ad wasn't loaded, skipping ad...");
uiManager.ProceedToGame();
}
}
// Update is called once per frame
void Update () {
}
}
我从来没有见过 OnAdFailedToLoad
方法被调用,我总是看到 OnAdLoaded
被调用,所以那里似乎没有问题。但是当我检查 ad.IsLoaded()
after 广告加载时,出于某种奇怪的原因,它是错误的。请注意,RewardBasedVideoAd
是 Google 文档中所述的单例对象。
我做错了什么?
更新: 我会在应用程序启动后立即调用 RequestNewAd()
并且在每次 "level" 启动时调用(想想它大概是一个级别~30 秒)和 DisplayAdOrProceed()
死亡(例如关卡结束)。当调用 DisplayAdOrProceed()
时,总是会调用广告的加载方法(除非我有连接问题,但这里不是这种情况)。
更新 2: 刚刚注意到 "ad load" 事件中有一个非常可疑的堆栈跟踪:
Ad loaded.
UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:Log(Object)
AdManager:Ad_OnAdLoaded(Object, EventArgs)
System.EventHandler`1:Invoke(Object, TEventArgs)
GoogleMobileAds.Api.RewardBasedVideoAd:<RewardBasedVideoAd>m__8(Object, AdFailedToLoadEventArgs)
System.EventHandler`1:Invoke(Object, TEventArgs)
GoogleMobileAds.iOS.RewardBasedVideoAdClient:RewardBasedVideoAdDidFailToReceiveAdWithErrorCallback(IntPtr, String)
我可以看到广告加载事件是从一个以失败处理程序命名的方法中触发的 (RewardBasedVideoAdDidFailToReceiveAdWithErrorCallback
、GoogleMobileAds.Api.RewardBasedVideoAd:<RewardBasedVideoAd>m__8(Object, AdFailedToLoadEventArgs)
)。但是,它调用的是广告加载事件,而不是任何失败处理程序。我不知道发生了什么,也不知道它是否是设计不当的命名约定。
好吧,在深入了解 Xcode 项目中由 IL2CPP 生成的 C++ 代码之后,我意识到 SDK 正在调用错误的方法。广告加载失败并出现错误 "No ad to show",但错误地启动了广告加载方法。我将对我的代码进行额外的更改以检测它是否被正确加载。
可怕的错误(或设计决策),Google。希望尽快解决这个问题。
我正在 Unity 5.5 上制作游戏,我已经按照 Google's Official AdMob Getting Started Guide 将 AdMob 集成到我的游戏中(目前仅 iOS,但 Android 也将在我得到这个工作)。
- 我已经在 AdMob 控制台设置了我的游戏并制作了一个基于奖励的视频广告。
- 我已将
GoogleMobileAds.framework
添加到我在 Xcode 中构建的项目中。 - 我已经确定我也在链接新添加的框架。
- 下载了Unity包,并根据需要集成到我的项目中。
- 我已经连接到事件处理程序:我的广告加载没有问题,没有失败。
当我尝试在 iOS 上显示广告时,ad.IsLoaded();
returns false 即使它刚刚加载(我的 OnAdLoaded
广告事件正在触发) .在 Unity 编辑器中,我看到以正确的顺序调用预期的虚拟方法(尽管没有显示任何内容,我认为这是 Unity 自己的编辑器的预期行为)。
这是我加载广告的代码(当然,我的发布商和广告单元 ID 已编辑):
public class AdManager : MonoBehaviour {
static RewardBasedVideoAd ad;
static UIManager uiManager;
#if UNITY_ANDROID
static string adUnitId = "ca-app-pub-XXX/YYY";
#elif UNITY_IPHONE
static string adUnitId = "ca-app-pub-XXX/YYY";
#else
static string adUnitId = "unexpected_platform";
#endif
static int headstartPoints;
// Use this for initialization
void Start () {
uiManager = GameObject.Find("Scripts").GetComponent<UIManager>();
ad = RewardBasedVideoAd.Instance;
ad.OnAdRewarded += Ad_OnAdRewarded;
ad.OnAdClosed += Ad_OnAdClosed;
ad.OnAdFailedToLoad += Ad_OnAdFailedToLoad;
ad.OnAdLoaded += Ad_OnAdLoaded;
headstartPoints = 0;
RequestNewAd();
}
void Ad_OnAdFailedToLoad (object sender, AdFailedToLoadEventArgs e)
{
Debug.Log("Ad failed to load.");
}
void Ad_OnAdLoaded (object sender, System.EventArgs e)
{
Debug.Log("Ad loaded.");
}
void Ad_OnAdClosed (object sender, System.EventArgs e)
{
Debug.Log("Ad was closed, proceeding to game without rewards...");
}
void Ad_OnAdRewarded (object sender, Reward e)
{
Debug.Log("Ad rewarded.");
headstartPoints = (int)e.Amount;
}
public static int GetAndConsumeRewards(){
int points = headstartPoints;
headstartPoints = 0;
return points;
}
public static void RequestNewAd(){
Debug.Log("Requested new ad.");
AdRequest request = new AdRequest.Builder().Build();
ad.LoadAd(request, adUnitId);
}
public static void DisplayAdOrProceed(){
if(ad.IsLoaded()){
ad.Show();
Debug.Log("Ad was loaded, displaying ad...");
}else{
Debug.Log("Ad wasn't loaded, skipping ad...");
uiManager.ProceedToGame();
}
}
// Update is called once per frame
void Update () {
}
}
我从来没有见过 OnAdFailedToLoad
方法被调用,我总是看到 OnAdLoaded
被调用,所以那里似乎没有问题。但是当我检查 ad.IsLoaded()
after 广告加载时,出于某种奇怪的原因,它是错误的。请注意,RewardBasedVideoAd
是 Google 文档中所述的单例对象。
我做错了什么?
更新: 我会在应用程序启动后立即调用 RequestNewAd()
并且在每次 "level" 启动时调用(想想它大概是一个级别~30 秒)和 DisplayAdOrProceed()
死亡(例如关卡结束)。当调用 DisplayAdOrProceed()
时,总是会调用广告的加载方法(除非我有连接问题,但这里不是这种情况)。
更新 2: 刚刚注意到 "ad load" 事件中有一个非常可疑的堆栈跟踪:
Ad loaded.
UnityEngine.DebugLogHandler:Internal_Log(LogType, String, Object)
UnityEngine.DebugLogHandler:LogFormat(LogType, Object, String, Object[])
UnityEngine.Logger:Log(LogType, Object)
UnityEngine.Debug:Log(Object)
AdManager:Ad_OnAdLoaded(Object, EventArgs)
System.EventHandler`1:Invoke(Object, TEventArgs)
GoogleMobileAds.Api.RewardBasedVideoAd:<RewardBasedVideoAd>m__8(Object, AdFailedToLoadEventArgs)
System.EventHandler`1:Invoke(Object, TEventArgs)
GoogleMobileAds.iOS.RewardBasedVideoAdClient:RewardBasedVideoAdDidFailToReceiveAdWithErrorCallback(IntPtr, String)
我可以看到广告加载事件是从一个以失败处理程序命名的方法中触发的 (RewardBasedVideoAdDidFailToReceiveAdWithErrorCallback
、GoogleMobileAds.Api.RewardBasedVideoAd:<RewardBasedVideoAd>m__8(Object, AdFailedToLoadEventArgs)
)。但是,它调用的是广告加载事件,而不是任何失败处理程序。我不知道发生了什么,也不知道它是否是设计不当的命名约定。
好吧,在深入了解 Xcode 项目中由 IL2CPP 生成的 C++ 代码之后,我意识到 SDK 正在调用错误的方法。广告加载失败并出现错误 "No ad to show",但错误地启动了广告加载方法。我将对我的代码进行额外的更改以检测它是否被正确加载。
可怕的错误(或设计决策),Google。希望尽快解决这个问题。