我正在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)
)。但是,它会调用广告加载事件,而不是任何失败处理程序。我不知道发生了什么,或者它是否是一个设计不佳的命名约定。
你在哪里调用DisplayAdOrProceed()和RequestNewAd()方法? –
@ m.rogalski在级别结束/级别开始。我已经更新了这个问题,以更详细地解决它们。 –