Unity RewardAd函数调用的时间不仅仅是一次

时间:2017-10-25 18:29:11

标签: c# unity3d admob reward

我想在游戏中使用RewardAd。当您观看视频时,您获得的目前得分为+10分,而不是您的高分。

你有45分高分,你现在是37分,所以你看视频为+10分,你有47分高分它很好。但如果你再次这样做,视频会给你+20分?!以下时间+30等等。

    using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using GoogleMobileAds.Api;
using System.Threading;

public class RewardAd : MonoBehaviour {

    public int highscore;
    public int score;
    public static GameObject drawscore_obj;
    public RewardBasedVideoAd rewardBasedVideo = null;
    public string adUnitId;

    void Start()
    {
        rewardBasedVideo = null;
        GetComponent<SpriteRenderer> ().enabled = false;
        //highscore = PlayerPrefs.GetInt ("highscore");
        adUnitId = "ca-app-pub-2879768424205988/1590886374";
        rewardBasedVideo = RewardBasedVideoAd.Instance;
        AdRequest request = new AdRequest.Builder().Build();
        rewardBasedVideo.LoadAd(request, adUnitId);
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
    }
    void Update()
    {
        if (GameOverScript.GameOver) 
        {
            GetComponent<SpriteRenderer> ().enabled = true;
        }
    }

    void OnMouseDown()
    {
        showAdd(rewardBasedVideo);
    }

    private void showAdd(RewardBasedVideoAd rewardBasedVideo)
    {
        if (rewardBasedVideo.IsLoaded())
        {
            if (GameOverScript.GameOver) 
            {
                rewardBasedVideo.Show ();
            }
        }
    }

    public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {   
        highscore = PlayerPrefs.GetInt ("highscore");

        if ((Controll.points + 10) > highscore) 
        {
            Controll.points += 10;
            if(Controll.points > highscore)
            {
                PlayerPrefs.SetInt ("highscore", highscore);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

如果你看一下这个方法:

private void showAdd(RewardBasedVideoAd rewardBasedVideo)
{
    if (rewardBasedVideo.IsLoaded())
    {
        if (GameOverScript.GameOver) 
        {
            rewardBasedVideo.Show ();
            rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        }
    }
}

每次调用它时,它都会向HandleRewardBasedVideoRewarded添加另一个OnAdRewarded的监听器。所以,它第一次调用HandleRewardBasedVideoRewarded一次;第二次两次;等等。

通常,您只需添加一次监听器:

void Start()
{
    rewardBasedVideo = null;
    GetComponent<SpriteRenderer> ().enabled = false;
    //highscore = PlayerPrefs.GetInt ("highscore");
    adUnitId = "ca-app-pub-2**97684242*****/15*08*****";
    rewardBasedVideo = RewardBasedVideoAd.Instance;
    AdRequest request = new AdRequest.Builder().Build();
    rewardBasedVideo.LoadAd(request, adUnitId);

    // Add a listener only when the script is loaded
    rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
}

private void showAdd(RewardBasedVideoAd rewardBasedVideo)
{
    if (rewardBasedVideo.IsLoaded())
    {
        if (GameOverScript.GameOver) 
        {
            rewardBasedVideo.Show ();
        }
    }
}

答案 1 :(得分:0)

我解决了我的问题!我认为它的一点点noob位于它的工作......:D

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using GoogleMobileAds.Api;
    using System.Threading;

    public class RewardAd : MonoBehaviour {

        public bool called;
        public int highscore;
        public int score;
        public static GameObject drawscore_obj;
        public RewardBasedVideoAd rewardBasedVideo = null;
        public string adUnitId;

        void Start()
        {
            called = false;
            rewardBasedVideo = null;
            GetComponent<SpriteRenderer> ().enabled = false;
            //highscore = PlayerPrefs.GetInt ("highscore");
            adUnitId = "ca-app-pub-2879768424205988/1590886374";
            rewardBasedVideo = RewardBasedVideoAd.Instance;
            AdRequest request = new AdRequest.Builder().Build();
            rewardBasedVideo.LoadAd(request, adUnitId);
            rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
        }
        void Update()
        {
            if (GameOverScript.GameOver) 
            {
                GetComponent<SpriteRenderer> ().enabled = true;
            }
        }

        void OnMouseDown()
        {
            showAdd(rewardBasedVideo);
        }

        private void showAdd(RewardBasedVideoAd rewardBasedVideo)
        {
            if (rewardBasedVideo.IsLoaded())
            {
                if (GameOverScript.GameOver) 
                {
                    rewardBasedVideo.Show ();
                    called = true;
                }
            }
        }

        public void HandleRewardBasedVideoRewarded(object sender, Reward args)
        {   
            if (called == true) 
            {
                called = false;
                highscore = PlayerPrefs.GetInt ("highscore");

                if ((Controll.points + 10) > highscore) {
                    Controll.points += 10;
                    if (Controll.points > highscore) {
                        PlayerPrefs.SetInt ("highscore", highscore);
                    }
                }
            }
         }
      }