我正在将Admob实施到我的Android统一游戏中。目前,我正在测试奖励视频广告。
我添加了ui文本,并希望在奖励视频播放完后更改文本。
但是在视频播放完并且我关闭了视频之后,文本没有改变。
这是我的剧本:
private RewardBasedVideoAd rewardBasedVideo;
public Text text;
// Start is called before the first frame update
void Start()
{
RequestReward();
}
private void RequestReward()
{
string adUnitId;
adUnitId = "ca-app-pub-3940256099942544/5224354917";
//rewardedAd = new RewardedAd(adUnitId);
rewardBasedVideo = RewardBasedVideoAd.Instance;
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded video ad with the request.
this.rewardBasedVideo.LoadAd(request, adUnitId);
}
public void DisplayRewardAd()
{
if (rewardBasedVideo.IsLoaded())
rewardBasedVideo.Show();
}
public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
{
rewardBasedVideo.Show();
}
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
RequestReward();
}
public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
text.text = "Done!";
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
text.text = "Done!";
}
public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
private void OnEnable()
{
HandleRewardedAdEvents(true);
}
private void OnDisable()
{
HandleRewardedAdEvents(false);
}
void HandleRewardedAdEvents(bool subscribe)
{
if (subscribe)
{
// Called when an ad request has successfully loaded.
rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
// Called when an ad request failed to load.
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
// Called when an ad is shown.
rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
// Called when the ad starts to play.
rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
// Called when the user should be rewarded for watching a video.
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
// Called when the ad is closed.
rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
// Called when the ad click caused the user to leave the application.
rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
}
else
{
// Called when an ad request has successfully loaded.
rewardBasedVideo.OnAdLoaded -= HandleRewardBasedVideoLoaded;
// Called when an ad request failed to load.
rewardBasedVideo.OnAdFailedToLoad -= HandleRewardBasedVideoFailedToLoad;
// Called when an ad is shown.
rewardBasedVideo.OnAdOpening -= HandleRewardBasedVideoOpened;
// Called when the ad starts to play.
rewardBasedVideo.OnAdStarted -= HandleRewardBasedVideoStarted;
// Called when the user should be rewarded for watching a video.
rewardBasedVideo.OnAdRewarded -= HandleRewardBasedVideoRewarded;
// Called when the ad is closed.
rewardBasedVideo.OnAdClosed -= HandleRewardBasedVideoClosed;
// Called when the ad click caused the user to leave the application.
rewardBasedVideo.OnAdLeavingApplication -= HandleRewardBasedVideoLeftApplication;
}
}
编辑:我忘了向问题添加HandleRewardedAdEvents方法。
答案 0 :(得分:2)
好了,现在您添加了HandleRewardedAdEvents
的实现,我将更改答案,并注意OnEnable
is executed before Start
,直到rewardBasedVideo
可能还不存在的那一刻。
所以我宁愿在其中添加回调
private void RequestReward()
{
string adUnitId;
adUnitId = "ca-app-pub-3940256099942544/5224354917";
//rewardedAd = new RewardedAd(adUnitId);
rewardBasedVideo = RewardBasedVideoAd.Instance;
AdRequest request = new AdRequest.Builder().Build();
// ADD THE CALLBACKS
HandleRewardedAdEvents(true);
// Load the rewarded video ad with the request.
this.rewardBasedVideo.LoadAd(request, adUnitId);
}
通常是的,您应该删除不再需要的任何回调。但是在这种情况下,我宁愿在
中进行操作private void OnDestroy()
{
HandleRewardedAdEvents(false);
}
并非每次都在OnEnable
和OnDisable
中使用。
并使用
private void HandleRewardedAdEvents(bool subscribe)
{
// NOTE that it is always secure to first remove callbacks
// even if not added yet
// this makes sure they are definitely only added once
// Called when an ad request has successfully loaded.
rewardBasedVideo.OnAdLoaded -= HandleRewardBasedVideoLoaded;
// Called when an ad request failed to load.
rewardBasedVideo.OnAdFailedToLoad -= HandleRewardBasedVideoFailedToLoad;
// Called when an ad is shown.
rewardBasedVideo.OnAdOpening -= HandleRewardBasedVideoOpened;
// Called when the ad starts to play.
rewardBasedVideo.OnAdStarted -= HandleRewardBasedVideoStarted;
// Called when the user should be rewarded for watching a video.
rewardBasedVideo.OnAdRewarded -= HandleRewardBasedVideoRewarded;
// Called when the ad is closed.
rewardBasedVideo.OnAdClosed -= HandleRewardBasedVideoClosed;
// Called when the ad click caused the user to leave the application.
rewardBasedVideo.OnAdLeavingApplication -= HandleRewardBasedVideoLeftApplication;
if (!subscribe) return;
// Called when an ad request has successfully loaded.
rewardBasedVideo.OnAdLoaded += HandleRewardBasedVideoLoaded;
// Called when an ad request failed to load.
rewardBasedVideo.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
// Called when an ad is shown.
rewardBasedVideo.OnAdOpening += HandleRewardBasedVideoOpened;
// Called when the ad starts to play.
rewardBasedVideo.OnAdStarted += HandleRewardBasedVideoStarted;
// Called when the user should be rewarded for watching a video.
rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
// Called when the ad is closed.
rewardBasedVideo.OnAdClosed += HandleRewardBasedVideoClosed;
// Called when the ad click caused the user to leave the application.
rewardBasedVideo.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
}