我正在 Unity 5.5 上制作游戏,并且已关注 Google's Official AdMob Getting Started Guide将 AdMob 集成到我的游戏中(目前仅限 iOS,但当我实现此功能后,Android 也会跟进)。
- 我已在 AdMob 控制台上设置了游戏并制作了奖励视频广告。
- 我添加了
GoogleMobileAds.framework
进入我在 Xcode 中构建的项目。 - 我已确保我也链接到新添加的框架。
- 下载了Unity包并根据需要将其集成到我的项目中。
- 我已连接到事件处理程序:我的广告加载时没有任何问题,也没有失败。
当我尝试在 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 秒)和 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)
)触发的。但是,它正在调用广告加载事件,而不是任何失败处理程序。我不知道发生了什么,也不知道这是否是一个设计不当的命名约定。
最佳答案
好吧,在深入研究 Xcode 项目中 IL2CPP 生成的 C++ 代码后,我意识到 SDK 调用了错误的方法。广告无法加载,并出现错误“无广告可显示”,但错误地启动了广告加载方法。我将对我的代码进行其他更改以检测它是否正确加载。
可怕的错误(或设计决策),Google。我希望这个问题能尽快得到解决。
关于c# - Unity AdMob 广告未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40974054/