unity-game-enginevideoadmobreward

How create multiple reward video's in Unity application?


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;
    }
}

Solution

  • 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.