我目前正在使用统一游戏引擎开发游戏。我正在使用谷歌的移动广告 SDK 来实施广告。根据文档,广告具有可用于自定义广告行为的生命周期事件。
出于我的目的,我正在使用插页式广告。当我在编辑器中运行该项目时,我可以访问这些事件并且我的代码按预期的方式工作。但是,当我构建 Android 时,这些广告生命周期事件不会触发,我无法让我的广告按照我想要的方式运行。
这是我的代码。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
using System;
using UnityEngine.Events;
public class interstitialAd : MonoBehaviour
{
public GameObject adFailedUI;
public gameBehavior gamebehavior;
public UnityEvent OnAdOpeningEvent;
public UnityEvent OnAdClosedEvent;
private InterstitialAd interstitial;
private bool adLoaded = false;
// Start is called before the first frame update
void Start()
{
RequestInterstitial();
}
// Update is called once per frame
void Update()
{
}
#region HELPER METHODS
private AdRequest CreateAdRequest()
{
return new AdRequest.Builder().Build();
}
#endregion
public void RequestInterstitial()
{
MonoBehaviour.print("Requesting Interstitial ad.");
string adUnitId = "ca-app-pub-3940256099942544/1033173712";
// Clean up interstitial before using it
if (interstitial != null)
{
interstitial.Destroy();
interstitial = null;
}
// Load an interstitial ad
InterstitialAd.Load(adUnitId, CreateAdRequest(),
(InterstitialAd ad, LoadAdError loadError) =>
{
if (loadError != null)
{
MonoBehaviour.print("Interstitial ad failed to load with error: " +
loadError.GetMessage());
return;
}
else if (ad == null)
{
MonoBehaviour.print("Interstitial ad failed to load.");
return;
}
MonoBehaviour.print("Interstitial ad loaded.");
interstitial = ad;
RegisterEventHandlers(interstitial);
});
}
private void RegisterEventHandlers(InterstitialAd ad)
{
ad.OnAdFullScreenContentOpened += () =>
{
MonoBehaviour.print("Interstitial ad opening.");
OnAdOpeningEvent.Invoke();
};
ad.OnAdFullScreenContentClosed += () =>
{
MonoBehaviour.print("Interstitial ad closed.");
OnAdClosedEvent.Invoke();
RequestInterstitial();
};
ad.OnAdImpressionRecorded += () =>
{
MonoBehaviour.print("Interstitial ad recorded an impression.");
};
ad.OnAdClicked += () =>
{
MonoBehaviour.print("Interstitial ad recorded a click.");
};
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
RequestInterstitial();
adFailedUI.SetActive(true);
MonoBehaviour.print("Interstitial ad failed to show with error: " +
error.GetMessage());
};
ad.OnAdPaid += (AdValue adValue) =>
{
string msg = string.Format("{0} (currency: {1}, value: {2}",
"Interstitial ad received a paid event.",
adValue.CurrencyCode,
adValue.Value);
MonoBehaviour.print(msg);
};
}
public void ShowAd()
{
if (this.interstitial.CanShowAd() && interstitial!=null)
{
this.interstitial.Show();
}
else
{
}
}
public void DestroyAd()
{
interstitial.Destroy();
}
public void OnDestroy()
{
DestroyAd();
}
}
请注意,在
RequestInterstitial
方法中,我添加了自己的调试消息以分析发生了什么。特别感兴趣的是消息 Requesting Interstitial ad.
和 Interstitial ad loaded.
。第一个在我们尝试加载广告时打印,另一个在过程成功时打印。
每当我在编辑器中运行该项目时,我都会收到
Interstitial ad loaded.
日志消息,确认我的广告已加载。但是,在我的移动版本中,我没有收到此消息。我得到的只是一个requesting interstitial ad.
看来,在我的移动构建中,代码无法超越这一点。
但是,我可以在我的移动版本中看到我的测试广告。只是我无法利用任何广告生命周期事件,例如
OnAdFullScreenContentClosed
或 OnAdFullScreenContentFailed
.
我正在使用由谷歌提供的测试广告单元 ID。我可以看到这些测试广告,但无法使用它们或与它们的事件互动。我也无法理解的是,既然广告已经明确加载(因为我可以看到),为什么我的移动日志不显示他们应该显示的
Interstitial ad loaded
消息?