简体   繁体   English

Unity AdMob广告未展示

[英]Unity AdMob ads not displayed

I'm making a game on Unity 5.5 and I've followed Google's Official AdMob Getting Started Guide to integrate AdMob into my game (currently iOS-only, but Android will also follow when I get this working). 我正在Unity 5.5上制作游戏,并且已遵循Google的《 AdMob官方入门指南》将AdMob集成到我的游戏中(目前仅适用于iOS,但在运行此功能时也将遵循Android)。

  • I've set up my game at the AdMob console and created a reward-based video ad. 我已经在AdMob控制台上设置了游戏,并制作了基于奖励的视频广告。
  • I've added the GoogleMobileAds.framework into my built project in Xcode. 我已经将GoogleMobileAds.framework添加到了Xcode构建的项目中。
  • I've made sure I'm linking against the newly-added framework too. 我确保我也要链接到新添加的框架。
  • Downloaded the Unity package and integrated it into my project as required. 下载了Unity软件包,并根据需要将其集成到我的项目中。
  • I've hooked up to event handlers: My ad gets loaded with no problem, no failure. 我已经联系了事件处理程序:我的广告加载没有问题,没有失败。

When I try to display the ad on iOS, ad.IsLoaded(); 当我尝试在iOS上展示广告时, ad.IsLoaded(); returns false even though it has just loaded (my OnAdLoaded event of the ad is firing). 即使刚刚加载(我的广告的OnAdLoaded事件正在触发)也返回false。 In Unity editor, I'm seeing the expected dummy methods being called at correct order (though nothing is being displayed, which I think is the expected behavior on Unity's own editor). 在Unity编辑器中,我看到正确的顺序调用了预期的虚拟方法(尽管未显示任何内容,我认为这是Unity自己的编辑器上的预期行为)。

Here is my code for loading ads (of course, my publisher and ad unit IDs are redacted): 这是我的广告加载代码(当然,我的发布商和广告单元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 () {

    }
}

I've never seen OnAdFailedToLoad method being called, and I always see OnAdLoaded being called, so there doesn't seem to be a problem there. 我从来没有见过OnAdFailedToLoad方法被调用,而且我总是看到OnAdLoaded被调用,所以那里似乎没有问题。 But when I check ad.IsLoaded() after ad is loaded, for some weird reason it's false. 但是,当我广告加载检查ad.IsLoaded() ,出于某种奇怪的原因,它是错误的。 Please note that RewardBasedVideoAd is a singleton object as stated in Google's docs. 请注意, RewardBasedVideoAd是Google文档中所述的单例对象。

What am I doing wrong? 我究竟做错了什么?

UPDATE: I am calling RequestNewAd() as soon as the app starts and also on each "level" start (think of it like one level is roughly ~30 seconds) and DisplayAdOrProceed() on death (eg end of the level). 更新:我在应用程序启动后立即调用RequestNewAd() ,也在每个“级别”启动时调用它(想像一个级别大约是30秒左右),而在死亡(例如级别结束RequestNewAd()时调用DisplayAdOrProceed() )。 When DisplayAdOrProceed() is called, the ad's loaded method is always called (unless I have a connection problem but that's not the case here). 调用DisplayAdOrProceed() ,始终会调用广告的已加载方法(除非我遇到连接问题,但此处不是这种情况)。

UPDATE 2: Just noticed that there is a really suspicious stack trace on "ad load" event: 更新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)

I can see that the ad load event is fired from a method named for a failure handler ( RewardBasedVideoAdDidFailToReceiveAdWithErrorCallback , GoogleMobileAds.Api.RewardBasedVideoAd:<RewardBasedVideoAd>m__8(Object, AdFailedToLoadEventArgs) ). 我可以看到广告加载事件是从为失败处理程序命名的方法( RewardBasedVideoAdDidFailToReceiveAdWithErrorCallbackGoogleMobileAds.Api.RewardBasedVideoAd:<RewardBasedVideoAd>m__8(Object, AdFailedToLoadEventArgs) )中GoogleMobileAds.Api.RewardBasedVideoAd:<RewardBasedVideoAd>m__8(Object, AdFailedToLoadEventArgs) However, it's calling the ad load event and not any failure handler. 但是,它调用的是广告加载事件,而不是任何故障处理程序。 I have no idea what's going on or if it's a poorly-designed naming convention or not. 我不知道这是怎么回事,或者这不是一个设计不良的命名约定。

Okay after diving literally into the IL2CPP-generated C++ code in the Xcode project, I've realized that the SDK is calling the incorrect method. 好吧,在深入研究Xcode项目中由IL2CPP生成的C ++代码之后,我意识到SDK正在调用错误的方法。 The ad is failing to load with error "No ad to show", but incorrectly firing up the ad load method. 广告无法加载,并显示错误“无广告可显示”,但错误地触发了广告加载方法。 I'll make additional changes to my code to detect if it's being loaded correctly. 我将对代码进行其他更改,以检测代码是否正确加载。

Terrible bug (or design decision), Google. 可怕的错误(或设计决策),Google。 I hope this gets fixed soon. 我希望这个问题能尽快解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM