【发布时间】:2016-12-05 12:13:55
【问题描述】:
我正在 Unity 5.5 上制作游戏,我已关注 Google's Official AdMob Getting Started Guide 将 AdMob 集成到我的游戏中(目前仅适用于 iOS,但当我完成这项工作时,Android 也会随之而来)。
- 我在 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 在关卡结束/关卡开始。我已经更新了问题以更详细地解决它们。
标签: c# ios unity3d admob rewardedvideoad