【问题标题】:How create multiple reward video's in Unity application?如何在 Unity 应用程序中创建多个奖励视频?
【发布时间】:2020-01-15 23:54:39
【问题描述】:

最近几天我试图在我的 Unity 应用中实现奖励视频 (admob)。我希望人们可以观看多个奖励视频,并提供不同类型的奖励。我觉得我很接近(也许根本没有),因为我几乎可以正常工作。第一次点击测试广告时,它会显示广告、我获得奖励以及我获得奖励的消息。如果我然后加载/观看第二个广告,它会起作用,但奖励不是应有的。我得到两个奖励。所以,我第一次看一个 100 个硬币的广告,我得到了 100 个硬币,而且效果很好。然后,我看了 500 个硬币的广告,但我得到了 600 个硬币,我收到的消息都是 100 和 500 个硬币,尽管我只“赚”了 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;
    }
}

【问题讨论】:

    标签: unity3d video admob reward


    【解决方案1】:

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

    您可以阅读更多关于它的信息here

    因为 RewardedBasedVideoAd 是单例,所以请求加载广告 应该使用共享实例。

    【讨论】:

    • 感谢您的帮助!但是,如果我只使用 1 个 RewardedBasedVideoAd,是否仍然可以有 2 个奖励视频,具有不同的奖励?这可能是一个愚蠢的问题,但我搜索了整个互联网。既然我只用小号,那么手柄就只给小号,大号没有奖励的地方。
    • 不是一个愚蠢的问题:)。您可以请求任意数量的广告。您不能一次请求多个。因此,如果您想展示“大”广告,您必须提出一个新请求,该请求将覆盖现有请求。
    • 您只需决定首先加载哪个广告以及何时加载其他广告。最后,当您挂钩到 rewardBasedVideo.OnAdRewarded 事件时,您将决定如何奖励您的用户
    【解决方案2】:

    使用标准的统一广告库让这一切变得容易得多,为什么不使用那个

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

    【讨论】:

    • 感谢您的提示,但我一直使用 admob,而且我已经有一些其他带有 admob 的应用程序,只是没有奖励视频。我想我已经在那里了,只需要改变一点点。我只是不明白它是什么。
    猜你喜欢
    • 1970-01-01
    • 2016-10-28
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多