我在Unity 5.5上制作游戏,然后我跟着Google's Official AdMob Getting Started Guide将AdMob集成到我的游戏中(目前只支持iOS,但是当我开始工作时,Android也会关注)。
GoogleMobileAds.framework
添加到我在Xcode中构建的项目中。当我尝试在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()
,并在每个&#34;等级&#34;开始(想想它就像一个级别约为30秒)和死亡时DisplayAdOrProceed()
(例如关卡结束时)。调用DisplayAdOrProceed()
后,系统会始终调用广告加载的方法(除非我遇到连接问题,但此处并非如此)。
更新2 :注意到&#34; ad load&#34;事件:
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 :(得分:4)
好吧,在Xcode项目中直接进入IL2CPP生成的C ++代码之后,我意识到SDK正在调用不正确的方法。广告无法加载错误“无广告展示”,但错误地启动了广告加载方法。我将对我的代码进行其他更改,以检测它是否正确加载。
可怕的错误(或设计决定),谷歌。我希望很快就能解决这个问题。