如何在Unity应用程序中创建多个奖励视频?

时间:2019-09-14 11:11:19

标签: unity3d video admob reward

最近几天,我试图在Unity应用中实施奖励视频(admob)。我想为视频的人们提供多种奖励,并提供不同类型的奖励。我感觉自己很接近(也许一点也不),因为我几乎可以正常工作。第一次点击测试广告时,它会显示该广告,我得到了奖励,并且得到了获得奖励的消息。如果我随后加载/观看第二个广告,则该广告有效,但奖励却不尽如人意。我都得到奖励。因此,我首先看一个广告,上面有100个金币,我得到100个金币,效果很好。然后,我观看了500个金币的广告,但我得到了600个金币,虽然我只“赚”了500个金币,但我收到的消息分别是100和500个金币。似乎HandleRewardBasedVideoRewardedAdMob两者之间都存在误区,但是我已经尝试了所有可以想到的方法,并且在互联网上没有发现任何类似的东西。我用100枚硬币作为小奖励,用500枚硬币作为大奖励。 我希望有人可以帮助我,因为这使我发疯。谢谢您的时间!

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 个答案:

答案 0 :(得分:1)

RewardBasedVideoAd是singelton对象(与BannerView和InterstitialAd不同)。在InitializeAds方法中,您将同一对象分配给RequestRewardedVideosmall和rewardBasedAdMobVideobig。您一次不能请求多个RewardBasedVideoAd。

您可以详细了解here

  

由于RewardedBasedVideoAd是单身人士,因此请求加载广告   应该使用共享实例创建。

答案 1 :(得分:0)

使用标准的统一广告库,一切都变得如此简单,为什么不使用该库呢?

这是带有奖励广告的统一广告: https://unityads.unity3d.com/help/unity/integration-guide-unity#rewarded-video-ads