i have a game and when the player loses their lives, i want them to be able to watch a video for a second chance.
I am using unity version 2018.1.1f1 person and i have downloaded the admob unity plugin version 3.13.1
So if the player agrees to watch an ad, the ad will play and then resume the game without firing the callback that rewards the player. This is my code:
using System;
using UnityEngine;
using GoogleMobileAds.Api;
using UnityEngine.UI;
public class RewardAd : MonoBehaviour {
private BannerView bannerView;
private InterstitialAd interstitial;
private RewardBasedVideoAd rewardBasedVideo;
private float deltaTime = 0.0f;
private static string outputMessage = string.Empty;
public AudioSource musicPlayer;
public Player player;
public Text UIText;
public static string OutputMessage
{
set { outputMessage = value; }
}
public void Start()
{
#if UNITY_ANDROID
string appId = "ca-app-pub-3940256099942544~3347511713";
#elif UNITY_IPHONE
string appId = "ca-app-pub-3940256099942544~1458002511";
#else
string appId = "unexpected_platform";
#endif
MobileAds.SetiOSAppPauseOnBackground(true);
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(appId);
//Get singleton reward based video ad reference.
this.rewardBasedVideo = RewardBasedVideoAd.Instance;
// RewardBasedVideoAd is a singleton, so handlers should only be registered once.
this.rewardBasedVideo.OnAdLoaded += this.HandleRewardBasedVideoLoaded;
this.rewardBasedVideo.OnAdFailedToLoad += this.HandleRewardBasedVideoFailedToLoad;
this.rewardBasedVideo.OnAdOpening += this.HandleRewardBasedVideoOpened;
this.rewardBasedVideo.OnAdStarted += this.HandleRewardBasedVideoStarted;
this.rewardBasedVideo.OnAdRewarded += this.HandleRewardBasedVideoRewarded;
this.rewardBasedVideo.OnAdClosed += this.HandleRewardBasedVideoClosed;
this.rewardBasedVideo.OnAdLeavingApplication += this.HandleRewardBasedVideoLeftApplication;
}
public void Update()
{
// Calculate simple moving average for time to render screen. 0.1 factor used as smoothing
// value.
this.deltaTime += (Time.deltaTime - this.deltaTime) * 0.1f;
}
// Returns an ad request with custom ad targeting.
private AdRequest CreateAdRequest()
{
return new AdRequest.Builder()
.AddTestDevice(AdRequest.TestDeviceSimulator)
.AddTestDevice("0123456789ABCDEF0123456789ABCDEF")
.AddKeyword("game")
.SetGender(Gender.Male)
.SetBirthday(new DateTime(1985, 1, 1))
.TagForChildDirectedTreatment(false)
.AddExtra("color_bg", "9B30FF")
.Build();
}
private void RequestRewardBasedVideo()
{
#if UNITY_EDITOR
string adUnitId = "unused";
#elif UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-7624023175090985/4535603801";
#else
string adUnitId = "unexpected_platform";
#endif
this.rewardBasedVideo.LoadAd(this.CreateAdRequest(), adUnitId);
}
private void ShowInterstitial()
{
if (this.interstitial.IsLoaded())
{
this.interstitial.Show();
}
else
{
MonoBehaviour.print("Interstitial is not ready yet");
}
}
private void ShowRewardBasedVideo()
{
if (this.rewardBasedVideo.IsLoaded())
{
this.rewardBasedVideo.Show();
}
else
{
MonoBehaviour.print("Reward based video ad is not ready yet");
}
}
#region RewardBasedVideo callback handlers
public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
{
this.rewardBasedVideo.Show();
UIText.text = "Watch a short video\nfor an extra life ?";
}
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
UIText.text = "Watch a short video\nfor an extra life ?";
}
public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
{
musicPlayer.Pause();
player.disableMovment = true;
}
public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
{
musicPlayer.Pause();
player.disableMovment = true;
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
UIText.text = "Watch a short video\nfor an extra life ?";
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
player.rewardAdUI.SetActive(false);
UIText.text = "Watch a short video\nfor an extra life ?";
player.disableMovment = false;
if (PlayerPrefs.GetInt("music") == 1)
{
musicPlayer.Play();
}
Lives.addLives(1);
player.RespawnPlayer();
}
public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
{
UIText.text = "Watch a short video\nfor an extra life ?";
}
public void secondChance()
{
UIText.text = "Loading video...\nplease wait.";
this.RequestRewardBasedVideo();
}
public void GameOver()
{
player.disableMovment = false;
if (PlayerPrefs.GetInt("music") == 1)
{
musicPlayer.Play();
}
player.gameOver();
}
#endregion
}
So the 2nd to last function called secondChance() is what runs to play the ad. On my android device, this plays the test rewarded video.
After the ad plays, the HandleRewardBasedVideoRewarded function should be called but it doesnt. Any help at all is appriciated
I found a Silent Error after debugging in Android Studio, its due to threading issues in Unity.
Solution : Call your handling event in main thread (in Update Method)
bool isAdClosed = false;
bool isRewarded = false;
void Update()
{
if (isAdClosed)
{
if (isRewarded)
{
// do all the actions
// reward the player
isRewarded = false;
}
else
{
// Ad closed but user skipped ads, so no reward
// Ad your action here
}
isAdClosed = false; // to make sure this action will happen only once.
}
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
print("HandleRewardBasedVideoClosed event received");
RequestRewardBasedVideo(); // to load Next Videoad
isAdClosed = true;
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
print("HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " +
type);
isRewarded = true;
}
Vivek's solution worked for me on Unity v20212.1.15f1, although I had to put the code in my GameManager Update function as the AdManager Update function wasn't running when the RewardedAd was shown.
Related
I recently launched a game in unity which I uploaded on playstore. I was able to display ads when I just built an apk from the app but when I upload the app bundle on playstore and playstore launched that update, the ads stop displaying. My admob account is approved and my individual app's approval status is also 'Ready'.
I am not able to understand why my game is not displaying any ads as it should be working. All the ad ids are correct and it works fine in the normal apk. I am seeing proper ads.
Can someone help me please.
My AdManager Script
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MediationManager : MonoBehaviour
{
#region Ad_IDs
public string app_Id;
public string banner_Id;
public string nativeBanner_Id;
public string interstitial_Id;
public string rewardedVideo_Id;
public static MediationManager Instance;
private BannerView bannerView;
private BannerView nativebannerView;
private InterstitialAd interstitial;
private RewardedAd rewardedAd;
#endregion
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(this);
print("again");
}
else
{
print("not again");
Destroy(this.gameObject);
}
}
// Start is called before the first frame update
void Start()
{
MobileAds.SetiOSAppPauseOnBackground(true);
//RequestConfiguration requestConfiguration =
// new RequestConfiguration.Builder()
// .SetTagForChildDirectedTreatment(TagForChildDirectedTreatment.True)
// .build();
//MobileAds.SetRequestConfiguration(requestConfiguration);
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(HandleInitCompleteAction);
}
private void HandleInitCompleteAction(InitializationStatus initstatus)
{
MobileAdsEventExecutor.ExecuteInUpdate(() =>
{
//RequestBanner();
RequestInterstitial();
RequestAdmobRewarded();
// RequestNativeBanner();
//ShowTopBanner();
});
}
#region Request Ads
public AdRequest CreateAdRequest()
{
return new AdRequest.Builder().Build();
}
private void RequestInterstitial()
{
if (PlayerPrefs.GetInt("adsRemoveCount") <= 2)
{
// Clean up interstitial ad before creating a new one.
if (this.interstitial != null)
{
this.interstitial.Destroy();
}
// Create an interstitial.
this.interstitial = new InterstitialAd(interstitial_Id);
// Register for ad events.
this.interstitial.OnAdLoaded += this.HandleOnAdLoaded;
this.interstitial.OnAdFailedToLoad += this.HandleOnAdFailedToLoad;
this.interstitial.OnAdOpening += this.HandleOnAdOpened;
this.interstitial.OnAdClosed += this.HandleOnAdClosed;
// this.interstitial. += this.HandleOnAdLeavingApplication;
// Load an interstitial ad.
this.interstitial.LoadAd(this.CreateAdRequest());
}
}
public void RequestBanner()
{
if (PlayerPrefs.GetInt("RemoveAds") == 0)
{
// Clean up banner ad before creating a new one.
if (this.bannerView != null)
{
this.bannerView.Destroy();
}
// Create a 320x50 banner at the top of the screen.
this.bannerView = new BannerView(banner_Id, AdSize.Banner, AdPosition.Top);
// Register for ad events.
this.bannerView.OnAdLoaded += this.HandleAdLoaded;
this.bannerView.OnAdFailedToLoad += this.HandleAdFailedToLoad;
this.bannerView.OnAdOpening += this.HandleAdOpened;
this.bannerView.OnAdClosed += this.HandleAdClosed;
//this.bannerView.OnAdLeavingApplication += this.HandleAdLeftApplication;
// Load a banner ad.
this.bannerView.LoadAd(this.CreateAdRequest());
this.bannerView.Hide();
print("Show Top Banner");
}
}
public void RequestNativeBanner()
{
if (PlayerPrefs.GetInt("RemoveAds") == 0)
{
if (this.nativebannerView != null)
{
this.nativebannerView.Destroy();
}
this.nativebannerView = new BannerView(banner_Id, AdSize.MediumRectangle, 0, 54);
this.nativebannerView.LoadAd(this.CreateAdRequest());
this.nativebannerView.Hide();
}
}
private void RequestAdmobRewarded()
{
if (PlayerPrefs.GetInt("adsRemoveCount") <= 2)
{
this.rewardedAd = new RewardedAd(rewardedVideo_Id);
// Called when an ad request has successfully loaded.
this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
// Called when an ad request failed to load.
// this.rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad;
// Called when an ad is shown.
this.rewardedAd.OnAdOpening += HandleRewardedAdOpening;
// Called when an ad request failed to show.
this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
// Called when the user should be rewarded for interacting with the ad.
this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
// Called when the ad is closed.
this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;
// Load the rewarded ad with the request.
this.rewardedAd.LoadAd(this.CreateAdRequest());
}
}
#endregion
#region Banner callback handlers
public void HandleAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLoaded event received");
}
public void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleFailedToReceiveAd event received with message: " + args.LoadAdError);
}
public void HandleAdOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdOpened event received");
}
public void HandleAdClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdClosed event received");
}
public void HandleAdLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLeftApplication event received");
}
#endregion
#region Interstial callback handlers
public void HandleOnAdLoaded(object sender, System.EventArgs args)
{
MonoBehaviour.print("HandleAdLoaded event received");
}
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleFailedToReceiveAd event received with message: "
+ args.ToString());
RequestInterstitial();
}
public void HandleOnAdOpened(object sender, System.EventArgs args)
{
MonoBehaviour.print("HandleAdOpened event received");
}
public void HandleOnAdClosed(object sender, System.EventArgs args)
{
MonoBehaviour.print("HandleAdClosed event received");
RequestInterstitial();
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
public void HandleOnAdLeavingApplication(object sender, System.EventArgs args)
{
MonoBehaviour.print("HandleAdLeavingApplication event received");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex - 1);
}
#endregion
#region AdmobRewarded_Ad callback handlers
public void HandleRewardedAdLoaded(object sender, System.EventArgs args)
{
MonoBehaviour.print("HandleRewardedAdLoaded event received");
}
public void HandleRewardedAdFailedToLoad(object sender, AdErrorEventArgs args)
{
MonoBehaviour.print(
"HandleRewardedAdFailedToLoad event received with message: "
+ args.AdError.GetMessage());
// RequestAdmobRewarded();
}
public void HandleRewardedAdOpening(object sender, System.EventArgs args)
{
MonoBehaviour.print("HandleRewardedAdOpening event received");
// RequestAdmobRewarded();
}
public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args)
{
MonoBehaviour.print(
"HandleRewardedAdFailedToShow event received with message: "
+ args.AdError.GetMessage());
// RequestAdmobRewarded();
}
public void HandleRewardedAdClosed(object sender, System.EventArgs args)
{
MonoBehaviour.print("HandleRewardedAdClosed event received");
RequestAdmobRewarded();
}
public void HandleUserEarnedReward(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
if (PlayerPrefs.HasKey("adsRemoveCount"))
{
PlayerPrefs.SetInt("adsRemoveCount", PlayerPrefs.GetInt("adsRemoveCount") + 1);
}
else
{
PlayerPrefs.SetInt("adsRemoveCount", 1);
}
}
#endregion
#region Ads Call Function
public void ShowAdmobInterstial()
{
if (PlayerPrefs.GetInt("adsRemoveCount") <= 2)
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
else
{
RequestInterstitial();
}
}
}
public void ShowAdmobRewardedVideo()
{
if (PlayerPrefs.GetInt("adsRemoveCount") <= 2)
{
if (this.rewardedAd.IsLoaded())
{
this.rewardedAd.Show();
}
else
{
RequestAdmobRewarded();
}
}
}
#endregion
}
When I watching rewarded on Unity, there is no problem but after building when I watching rewarded, I don't get any rewards. I tried so hard and I can't find. Can you help me guys? Thank you in advance.
public class AdReward : MonoBehaviour
{
private RewardedAd rewardedAd;
int rewardAmount;
int coinn;
private void Start()
{
coinn= PlayerPrefs.GetInt("NumberOfCoins");
string adUnitId;
#if UNITY_ANDROID
adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
adUnitId = "unexpected_platform";
#endif
MobileAds.Initialize(initStatus => { });
this.rewardedAd = new RewardedAd(adUnitId);
rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded ad with the request.
this.rewardedAd.LoadAd(request);
}
public void HandleUserEarnedReward(object sender, Reward args)
{
double amount = args.Amount;
coinn += (int)amount*20;
PlayerPrefs.SetInt("NumberOfCoins", coinn);
}
public void UserChoseToWatchAd()
{
if (this.rewardedAd.IsLoaded())
{
this.rewardedAd.Show();
}
}
}
I am using admob ads in my app and they are working fine. But when i try to do something after a ad close or reward earned call back my code breaks. Following is my adMob script
public class AdMobScript : MonoBehaviour
{
...
public event Action OnReviveRewardEarned;
public event Action OnReviveAdLoaded;
public event Action OnReviveAdClosed;
private void LoadReviveRewardedAd()
{
reviveRewardedAd = new RewardedAd(adReviveRewardedId);
reviveRewardedAd.OnAdLoaded += ReviveAdLoaded;
reviveRewardedAd.OnUserEarnedReward += ReviveEarnedReward;
reviveRewardedAd.OnAdClosed += ReviveAdClosed;
AdRequest request = new AdRequest.Builder().Build();
reviveRewardedAd.LoadAd(request);
}
private void ReviveAdClosed(object sender, EventArgs e)
{
LoadReviveRewardedAd();
if (isRewardErned)
{
isRewardErned = false;
OnReviveRewardEarned.Invoke();
}
else
OnReviveAdClosed.Invoke();
}
private void ReviveEarnedReward(object sender, Reward e)
{
isRewardErned = true;
}
private void ReviveAdLoaded(object sender, EventArgs e)
{
//reviveButton.interactable = true;
OnReviveAdLoaded.Invoke();
}
public void ShowAdToRevive()
{
if (reviveRewardedAd.IsLoaded())
{
reviveRewardedAd.Show();
}
}
...
}
In the callbacks i am calling my adManager script which is in term using adMob script. Here is the code for it.
public class AdManager : MonoBehaviour
{
...
private void Start() {
AdMobScript.instance.OnReviveAdClosed += ReviveAdClosed;
AdMobScript.instance.OnReviveAdLoaded += ReviveAdLoaded;
AdMobScript.instance.OnReviveRewardEarned += ReviveReward;
}
#region ReviveAds
private void ReviveReward() {
//game crash here
backButton.gameObject.SetActive(true);
reviveButton.gameObject.SetActive(false);
noThanksButton.gameObject.SetActive(false);
manager.Revive();
}
private void ReviveAdLoaded() {
reviveButton.interactable = true;
}
private void ReviveAdClosed() {
//game crash here
reviveButton.interactable = false;
}
public void ShowAdToRevive() {
AdMobScript.instance.ShowAdToRevive();
}
...
}
After either ad close or reward earned my game crashes (error log says
get_gameObject can only be called from the main thread
). There must be something i am doing wrong. Can anyone please point me to the right direction?
The reason for the problem - you trying to manipulate with MonoBehaviors, not in main thread.
Just write simple scheduler which calls the events in the Unity thread, like this:
Scheduler:
using System;
using UnityEngine;
public class Scheduler : MonoBehaviour
{
public static Scheduler instance;
public event Action secondTick = delegate { };
private float seconds = 0;
private void Awake()
{
instance = this;
}
private void Update()
{
seconds += Time.unscaledDeltaTime;
if (seconds >= 1.0f)
{
seconds -= 1.0f;
secondTick.Invoke();
}
}
}
Updated AdMobScript:
public class AdMobScript : MonoBehaviour
{
...
private bool onRewardEarnedCall = false;
private bool onRewardAdLoaded = false;
private bool onRewardAdClosed = false;
public event Action OnReviveRewardEarned;
public event Action OnReviveAdLoaded;
public event Action OnReviveAdClosed;
private void LoadReviveRewardedAd()
{
reviveRewardedAd = new RewardedAd(adReviveRewardedId);
reviveRewardedAd.OnAdLoaded += ReviveAdLoaded;
reviveRewardedAd.OnUserEarnedReward += ReviveEarnedReward;
reviveRewardedAd.OnAdClosed += ReviveAdClosed;
AdRequest request = new AdRequest.Builder().Build();
reviveRewardedAd.LoadAd(request);
Scheduler.instance.secondTick += OnSecondTick;
}
private void OnSecondTick()
{
if(onRewardAdClosed)
{
onRewardAdClosed = false;
OnReviveAdClosed.Invoke();
}
if(onRewardEarnedCall)
{
OnReviveRewardEarned.Invoke();
onRewardEarnedCall = false;
}
if(onRewardAdLoaded)
{
OnReviveAdLoaded.Invoke();
onRewardAdLoaded = false;
}
}
private void ReviveAdClosed(object sender, EventArgs e)
{
LoadReviveRewardedAd();
if (isRewardErned)
{
isRewardErned = false;
onRewardEarnedCall = true;
}
else
onRewardAdClosed = true;
}
private void ReviveEarnedReward(object sender, Reward e)
{
isRewardErned = true;
}
private void ReviveAdLoaded(object sender, EventArgs e)
{
//reviveButton.interactable = true;
onRewardAdLoaded = true;
}
public void ShowAdToRevive()
{
if (reviveRewardedAd.IsLoaded())
{
reviveRewardedAd.Show();
}
}
...
}
This is a very simple and not optimize solution but this will solve your problem.
the last few days I am trying to implement reward video's (admob) in my Unity app. I want to have multiple rewards video's people can watch, with different types of rewards. I feel like I am close (maybe not at all), since I have it working almost correctly. The first time a click on a test ad, it shows the ad, I get the reward and the message that I got the reward. If I then load/watch the second ad, it works, but the reward is not as it should be. I get both rewards. So, I first watch an ad for 100 coins, I get 100 coins, and it works perfectly. Then, I watch the 500 coins ad, but I get 600 coins, and both the messages that I received 100, and 500 coins, although I only 'earned' the 500 coins. It looks like there is a misstake somewhere with both the HandleRewardBasedVideoRewardedAdMob, but I have tried everything I can think of, and I have not found anything similar on the internet. I used small for 100 coins reward, and big for 500 coins.
I hope someone can help me out, since it is making me crazy. Thank you for your time!
using UnityEngine;
using GoogleMobileAds.Api;
using System;
public class AdsManager : MonoBehaviour {
#region AdMob
[Header("Admob")]
private string adMobAppID = "";
private string videoAdMobIdsmall = "ca-app-pub-3940256099942544/5224354917";
private string videoAdMobIdbig = "ca-app-pub-3940256099942544/5224354917";
private RewardBasedVideoAd rewardBasedAdMobVideosmall;
private RewardBasedVideoAd rewardBasedAdMobVideobig;
AdRequest AdMobVideoRequestsmall;
AdRequest AdMobVideoRequestbig;
#endregion
[Space(15)]
public decimal moneyToAddsmall;
public decimal moneyToAddbig;
static AdsManager instance;
public static AdsManager Instance
{
get
{
if(instance == null)
instance = GameObject.FindObjectOfType(typeof(AdsManager)) as AdsManager;
return instance;
}
}
void Awake ()
{
gameObject.name = this.GetType().Name;
DontDestroyOnLoad(gameObject);
InitializeAds();
}
public void ShowVideoRewardsmall() {
moneyToAddsmall = 100;
if(rewardBasedAdMobVideosmall.IsLoaded())
{
AdMobShowVideosmall();
}
}
public void ShowVideoRewardbig() {
moneyToAddbig = 500;
if(rewardBasedAdMobVideobig.IsLoaded())
{
AdMobShowVideobig();
}
}
private void RequestRewardedVideosmall()
{
// Called when an ad request has successfully loaded.
rewardBasedAdMobVideosmall.OnAdLoaded += HandleRewardBasedVideoLoadedAdMobsmall;
// Called when an ad request failed to load.
rewardBasedAdMobVideosmall.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoadAdMobsmall;
// Called when an ad is shown.
rewardBasedAdMobVideosmall.OnAdOpening += HandleRewardBasedVideoOpenedAdMobsmall;
// Called when the ad starts to play.
rewardBasedAdMobVideosmall.OnAdStarted += HandleRewardBasedVideoStartedAdMobsmall;
// Called when the user should be rewarded for watching a video.
rewardBasedAdMobVideosmall.OnAdRewarded += HandleRewardBasedVideoRewardedAdMobsmall;
// Called when the ad is closed.
rewardBasedAdMobVideosmall.OnAdClosed += HandleRewardBasedVideoClosedAdMobsmall;
// Called when the ad click caused the user to leave the application.
rewardBasedAdMobVideosmall.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplicationAdMobsmall;
// Create an empty ad request.
AdMobVideoRequestsmall = new AdRequest.Builder().Build();
// Load the rewarded video ad with the request.
this.rewardBasedAdMobVideosmall.LoadAd(AdMobVideoRequestsmall, videoAdMobIdsmall);
}
public void HandleRewardBasedVideoLoadedAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoadAdMobsmall(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
}
public void HandleRewardBasedVideoOpenedAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStartedAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosedAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
this.rewardBasedAdMobVideosmall.LoadAd(AdMobVideoRequestsmall, videoAdMobIdsmall);
}
public void HandleRewardBasedVideoRewardedAdMobsmall(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
Statistics._instance.AddMoney(moneyToAddsmall);
Toast.instance.ShowMessage("Congratulations with your 100 coins!", 4);
MonoBehaviour.print("HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " + type);
}
public void HandleRewardBasedVideoLeftApplicationAdMobsmall(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
private void RequestRewardedVideobig()
{
// Called when an ad request has successfully loaded.
rewardBasedAdMobVideobig.OnAdLoaded += HandleRewardBasedVideoLoadedAdMobbig;
// Called when an ad request failed to load.
rewardBasedAdMobVideobig.OnAdFailedToLoad += HandleRewardBasedVideoFailedToLoadAdMobbig;
// Called when an ad is shown.
rewardBasedAdMobVideobig.OnAdOpening += HandleRewardBasedVideoOpenedAdMobbig;
// Called when the ad starts to play.
rewardBasedAdMobVideobig.OnAdStarted += HandleRewardBasedVideoStartedAdMobbig;
// Called when the user should be rewarded for watching a video.
rewardBasedAdMobVideobig.OnAdRewarded += HandleRewardBasedVideoRewardedAdMobbig;
// Called when the ad is closed.
rewardBasedAdMobVideobig.OnAdClosed += HandleRewardBasedVideoClosedAdMobbig;
// Called when the ad click caused the user to leave the application.
rewardBasedAdMobVideobig.OnAdLeavingApplication += HandleRewardBasedVideoLeftApplicationAdMobbig;
// Create an empty ad request.
AdMobVideoRequestbig = new AdRequest.Builder().Build();
// Load the rewarded video ad with the request.
this.rewardBasedAdMobVideobig.LoadAd(AdMobVideoRequestbig, videoAdMobIdbig);
}
public void HandleRewardBasedVideoLoadedAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoadAdMobbig(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoFailedToLoad event received with message: " + args.Message);
}
public void HandleRewardBasedVideoOpenedAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStartedAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosedAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoClosed event received");
this.rewardBasedAdMobVideosmall.LoadAd(AdMobVideoRequestbig, videoAdMobIdsmall);
}
public void HandleRewardBasedVideoRewardedAdMobbig(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
Statistics._instance.AddMoney(moneyToAddbig);
Toast.instance.ShowMessage("Congratulations with your 500 coins!", 4);
MonoBehaviour.print("HandleRewardBasedVideoRewarded event received for " + amount.ToString() + " " + type);
}
public void HandleRewardBasedVideoLeftApplicationAdMobbig(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardBasedVideoLeftApplication event received");
}
void InitializeAds()
{
MobileAds.Initialize(adMobAppID);
this.rewardBasedAdMobVideosmall = RewardBasedVideoAd.Instance;
this.RequestRewardedVideosmall();
this.rewardBasedAdMobVideobig = RewardBasedVideoAd.Instance;
this.RequestRewardedVideobig();
}
void AdMobShowVideosmall()
{
rewardBasedAdMobVideosmall.Show();
}
void AdMobShowVideobig()
{
rewardBasedAdMobVideobig.Show();
}
bool isVideoAvaiable()
{
#if !UNITY_EDITOR
if(rewardBasedAdMobVideosmall.IsLoaded())
{
return true;
}
#endif
return false;
}
bool isVideoAvaiablebig()
{
#if !UNITY_EDITOR
if(rewardBasedAdMobVideobig.IsLoaded())
{
return true;
}
#endif
return false;
}
}
RewardBasedVideoAd is a singelton object (unlike BannerView and InterstitialAd). In your InitializeAds method you are assigning same object to RequestRewardedVideosmall and rewardBasedAdMobVideobig. You cannot request more than one RewardBasedVideoAd at the time.
You can read more about it here
Because RewardedBasedVideoAd is a singleton, requests to load an ad
should be made using a shared instance.
this is all so much easier with the standard unity ads library why not use that one
this is unity ads with rewarded ads:
https://unityads.unity3d.com/help/unity/integration-guide-unity#rewarded-video-ads
interstitialAd.IsLoaded() is always returning true even if device is offline. its works fine when app device is online.
public class InsAdBanner : MonoBehaviour
{
public int SceneIndex;
private InterstitialAd interstitialAd;
void Start()
{
this.RequestInterstitial();
}
void RequestInterstitial()
{
string interstitial_ID = "ad_id";
interstitialAd = new InterstitialAd(interstitial_ID);
if (this.interstitialAd != null)
{
this.interstitialAd.Destroy();
}
this.interstitialAd.OnAdLoaded += HandleOnAdLoaded;
this.interstitialAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
this.interstitialAd.OnAdOpening += HandleOnAdOpened;
this.interstitialAd.OnAdClosed += HandleOnAdClosed;
this.interstitialAd.OnAdLeavingApplication += HandleOnAdLeavingApplication;
AdRequest adRequest = new AdRequest.Builder().Build();
interstitialAd.LoadAd(adRequest);
}
public void Display_InterstitialAD()
{
<!-- plz see this line below -->
if (interstitialAd.IsLoaded())
{
Debug.Log("interstitialAd " + interstitialAd.IsLoaded());
interstitialAd.Show();
}
else {
getOut();
}
}
public void getOut() {
interstitialAd.Destroy();
SceneManager.LoadScene(SceneIndex);
restertAdCounter.restertsAdCouner += 1;
}
#region Interstitial callback handlers
//Handle event
public void HandleOnAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLoaded event received");
}
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
RequestInterstitial();
}
public void HandleOnAdOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdOpened event received");
}
public void HandleOnAdClosed(object sender, EventArgs args)
{
SceneManager.LoadScene(SceneIndex);
}
public void HandleOnAdLeavingApplication(object sender, EventArgs args)
{
SceneManager.LoadScene(SceneIndex);
}
#endregion
void OnDisable()
{
this.interstitialAd.OnAdLoaded += HandleOnAdLoaded;
this.interstitialAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
this.interstitialAd.OnAdOpening += HandleOnAdOpened;
this.interstitialAd.OnAdClosed += HandleOnAdClosed;
this.interstitialAd.OnAdLeavingApplication += HandleOnAdLeavingApplication;
}
void OnDestroy()
{
interstitialAd.Destroy();
}
}
I expecting it to return false in offline..it is working fine when app device is online .. but creates this problem when device is offline..plz help.. i cant find any solution in google.
Edit: Its not working on online too.. after adding real ad in place of test ad this problem is occurring.
Solved my problem by changing Minify->Relese->Proguard to Minify->Relese->None on player setting.(Although i dont know how actually its working)