I have been stuck with this problem for many days. I am trying to add rewarded video to my game but the rewarded video events are not being called. The video ads are shown properly but the events are not being called. I have to disable a UI gameObject
too; it is very necessary for HandleRewardBasedVideoRewarded
to work properly.
I have tried to look for a free demo project where HandleRewardBasedVideoRewarded
is being called and some work is being done, but couldn't find any such projects. I have looked for some asset in the unity asset store where admob is properly integrated in a basic game so that I could follow its code and correct my code, but I can't find such asset. I really need this to work. The project I am working on is almost complete only this problem remains.
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
I also tried to implement rewarded video event in other way. In that way, the onRewarded
video event was called and everything was working perfectly fine the first time, but it didn't work second time I tried to load the scene.