2

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

2 Answers2

2

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.

Community
  • 1
  • 1
Dave
  • 2,684
  • 2
  • 21
  • 38
  • Thanks, for the help! But if I use only 1 RewardedBasedVideoAd, is it still possible to have 2 rewarded video's, with different rewards? This probably is a stupid question, but I searched the whole internet. Since I use only small for example, then the handle will be for small only, and there is no place to reward for big. – DaanNetherlands Sep 14 '19 at 12:47
  • Not a stupid question :). You can request as many ads as you want. You just can't request multiple at once. So in case you you want to show the "big" ad you have to make a new request which will override the existing one. – Dave Sep 14 '19 at 12:52
  • You just have to decide which ad should load first and when other ads should be loaded. And in the end you decide how your user will be rewarded when you hook into rewardBasedVideo.OnAdRewarded event – Dave Sep 14 '19 at 12:57
0

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

  • Thanks for the tip, but I always used admob, and I already have some other apps with admob, just not rewarded video's. And I think I am already there, just need to change one little thing. I just do not see what it is. – DaanNetherlands Sep 14 '19 at 12:02