[英]Unity AdMob ads not displayed
我正在Unity 5.5上制作游戲,並且已遵循Google的《 AdMob官方入門指南》將AdMob集成到我的游戲中(目前僅適用於iOS,但在運行此功能時也將遵循Android)。
GoogleMobileAds.framework
添加到了Xcode構建的項目中。 當我嘗試在iOS上展示廣告時, ad.IsLoaded();
即使剛剛加載(我的廣告的OnAdLoaded
事件正在觸發)也返回false。 在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秒左右),而在死亡(例如級別結束RequestNewAd()
時調用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)
)中GoogleMobileAds.Api.RewardBasedVideoAd:<RewardBasedVideoAd>m__8(Object, AdFailedToLoadEventArgs)
。 但是,它調用的是廣告加載事件,而不是任何故障處理程序。 我不知道這是怎么回事,或者這不是一個設計不良的命名約定。
好吧,在深入研究Xcode項目中由IL2CPP生成的C ++代碼之后,我意識到SDK正在調用錯誤的方法。 廣告無法加載,並顯示錯誤“無廣告可顯示”,但錯誤地觸發了廣告加載方法。 我將對代碼進行其他更改,以檢測代碼是否正確加載。
可怕的錯誤(或設計決策),Google。 我希望這個問題能盡快解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.