2016-12-05 210 views
2

我正在Unity 5.5上製作遊戲,並且我已跟隨Google's Official AdMob Getting Started Guide將AdMob集成到我的遊戲中(目前僅限iOS,但Android也會在我得到此作品時關注)。未顯示Unity AdMob廣告

  • 我在AdMob控制檯設置了我的遊戲並創建了基於獎勵的視頻廣告。
  • 我已將GoogleMobileAds.framework添加到我在Xcode中構建的項目中。
  • 我已經確定我正在鏈接新增框架。
  • 下載Unity軟件包,並根據需要將其集成到我的項目中。
  • 我已經聯繫到事件處理程序:我的廣告加載沒有問題,沒有失敗。

當我嘗試顯示在iOS上的廣告,即使它只是加載ad.IsLoaded();返回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()時,出於某種奇怪的原因它是錯誤的。請注意,RewardBasedVideoAd是Google文檔中所述的單例對象。

我在做什麼錯?

更新:我馬上打電話RequestNewAd()爲應用程序啓動時,也對每一個「級別」開始(認爲它像一個水平大致〜30秒)和DisplayAdOrProceed()死亡(例如水平的結束) 。當調用DisplayAdOrProceed()時,廣告的加載方法總是被調用(除非我有連接問題,但這裏不是這種情況)。

更新2:只注意到有關於「加載廣告」事件確實可疑的堆棧跟蹤:

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))。但是,它會調用廣告加載事件,而不是任何失敗處理程序。我不知道發生了什麼,或者它是否是一個設計不佳的命名約定。

+0

你在哪裏調用DisplayAdOrProceed()和RequestNewAd()方法? –

+0

@ m.rogalski在級別結束/級別開始。我已經更新了這個問題,以更詳細地解決它們。 –

回答

4

好了,在Xcode項目中潛入IL2CPP生成的C++代碼後,我意識到SDK正在調用不正確的方法。該廣告未能加載錯誤「無廣告顯示」,但錯誤地啓動了廣告載入方式。我將對我的代碼進行其他更改以檢測它是否正確加載。

可怕的錯誤(或設計決定),谷歌。我希望很快就會得到解決。