我被这个问题困扰了很多天。我正在尝试将奖励视频添加到我的游戏中,但没有调用奖励视频事件。视频广告正确显示,但未调用事件。我也必须禁用 UI gameObject
;正常工作是非常必要的HandleRewardBasedVideoRewarded
。
我试图寻找一个免费的演示项目,其中HandleRewardBasedVideoRewarded
正在调用并且正在完成一些工作,但找不到任何此类项目。我在统一资产商店中寻找了一些资产,其中 admob 已正确集成到基本游戏中,以便我可以遵循它的代码并更正我的代码,但我找不到这样的资产。我真的需要这个工作。我正在做的项目几乎完成了,只剩下这个问题。
using System.Collections;
using System.Collections.Generic;
using System;
using UnityEngine;
using GoogleMobileAds.Api;
public class ADManager : MonoBehaviour {
private string APP_ID = "ca-app-pub-4889382687991550~5546461991";
private BannerView bannerAD;
private InterstitialAd interstitialAd;
private RewardBasedVideoAd rewardVideoAd;
public GameObject testSprite;
void Start() {
// this is when you publish your app
//MobileAds.Initialize(APP_ID);
RequestBanner();
RequestInterstitial();
RequestVideoAD();
}
void RequestBanner() {
string banner_ID = "ca-app-pub-3940256099942544/6300978111";
bannerAD = new BannerView(banner_ID, AdSize.SmartBanner, AdPosition.Bottom);
// FOR REAL APP
//AdRequest adRequest = new AdRequest.Builder().Build();
// FOR TESTING
AdRequest adRequest = new AdRequest.Builder()
.AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build();
bannerAD.LoadAd(adRequest);
}
void RequestInterstitial() {
string interstitial_ID = "ca-app-pub-3940256099942544/1033173712";
interstitialAd = new InterstitialAd(interstitial_ID);
// FOR REAL APP
//AdRequest adRequest = new AdRequest.Builder().Build();
// FOR TESTING
AdRequest adRequest = new AdRequest.Builder()
.AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build();
interstitialAd.LoadAd(adRequest);
}
void RequestVideoAD() {
string video_ID = "ca-app-pub-3940256099942544/5224354917";
rewardVideoAd = RewardBasedVideoAd.Instance;
// FOR REAL APP
//AdRequest adRequest = new AdRequest.Builder().Build();
// FOR TESTING
AdRequest adRequest = new AdRequest.Builder()
.AddTestDevice("2077ef9a63d2b398840261c8221a0c9b").Build();
rewardVideoAd.LoadAd(adRequest, video_ID);
}
public void Display_Banner() {
bannerAD.Show();
}
public void Display_InterstitialAD() {
if (interstitialAd.IsLoaded()) {
interstitialAd.Show();
}
}
public void Display_Reward_Video() {
if(rewardVideoAd.IsLoaded()) {
rewardVideoAd.Show();
}
}
// HANDLE EVENTS
public void HandleOnAdLoaded(object sender, EventArgs args) {
// ad is loaded show it
Display_Banner();
}
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) {
// ad failed to load load it again
RequestBanner();
}
public void HandleOnAdOpened(object sender, EventArgs args) {
MonoBehaviour.print("HandleAdOpened event received");
}
public void HandleOnAdClosed(object sender, EventArgs args) {
MonoBehaviour.print("HandleAdClosed event received");
}
public void HandleOnAdLeavingApplication(object sender, EventArgs args) {
MonoBehaviour.print("HandleAdLeavingApplication event received");
}
void HandleBannerADEvents(bool subscribe) {
if(subscribe) {
// Called when an ad request has successfully loaded.
bannerAD.OnAdLoaded += HandleOnAdLoaded;
// Called when an ad request failed to load.
bannerAD.OnAdFailedToLoad += HandleOnAdFailedToLoad;
// Called when an ad is clicked.
bannerAD.OnAdOpening += HandleOnAdOpened;
// Called when the user returned from the app after an ad click.
bannerAD.OnAdClosed += HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
bannerAD.OnAdLeavingApplication += HandleOnAdLeavingApplication;
} else {
// Called when an ad request has successfully loaded.
bannerAD.OnAdLoaded -= HandleOnAdLoaded;
// Called when an ad request failed to load.
bannerAD.OnAdFailedToLoad -= HandleOnAdFailedToLoad;
// Called when an ad is clicked.
bannerAD.OnAdOpening -= HandleOnAdOpened;
// Called when the user returned from the app after an ad click.
bannerAD.OnAdClosed -= HandleOnAdClosed;
// Called when the ad click caused the user to leave the application.
bannerAD.OnAdLeavingApplication -= HandleOnAdLeavingApplication;
}
}
void OnEnable() {
HandleBannerADEvents(true);
HandleRewardedADEvents(true);
}
void OnDisable() {
HandleBannerADEvents(false);
HandleRewardedADEvents(false);
}
//Rewarded Video events
public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print(
"HandleRewardBasedVideoFailedToLoad event received with message: "
+ args.Message);
}
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)
{
MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
testSprite.SetActive(false);
}
public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
void HandleRewardedADEvents(bool subscribe)
{
if (subscribe)
{
// Called when an ad request has successfully loaded.
rewardVideoAd.OnAdLoaded += HandleRewardBasedVideoLoaded;
// Called when an ad request failed to load.
rewardVideoAd.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoad;
// Called when an ad is shown.
rewardVideoAd.OnAdOpening += HandleRewardBasedVideoOpened;
// Called when the ad starts to play.
rewardVideoAd.OnAdStarted += HandleRewardBasedVideoStarted;
// Called when the user should be rewarded for watching a video.
rewardVideoAd.OnAdRewarded += HandleRewardBasedVideoRewarded;
// Called when the ad is closed.
rewardVideoAd.OnAdClosed += HandleRewardBasedVideoClosed;
// Called when the ad click caused the user to leave the application.
rewardVideoAd.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplication;
}
else
{
// Called when an ad request has successfully loaded.
rewardVideoAd.OnAdLoaded -= HandleRewardBasedVideoLoaded;
// Called when an ad request failed to load.
rewardVideoAd.OnAdFailedToLoad -= HandleRewardBasedVideoFailedToLoad;
// Called when an ad is shown.
rewardVideoAd.OnAdOpening -= HandleRewardBasedVideoOpened;
// Called when the ad starts to play.
rewardVideoAd.OnAdStarted -= HandleRewardBasedVideoStarted;
// Called when the user should be rewarded for watching a video.
rewardVideoAd.OnAdRewarded -= HandleRewardBasedVideoRewarded;
// Called when the ad is closed.
rewardVideoAd.OnAdClosed -= HandleRewardBasedVideoClosed;
// Called when the ad click caused the user to leave the application.
rewardVideoAd.OnAdLeavingApplication -= HandleRewardBasedVideoLeftApplication;
}
}
} // class
我还尝试以其他方式实施奖励视频事件。这样,onRewarded
视频事件被调用并且第一次一切正常,但第二次我尝试加载场景时它没有工作。