2

我想在我的游戏中加入 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);
            }
        }
    }
}
4

2 回答 2

2

如果你看这个方法:

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

每次调用它都会添加另一个 to 的监听HandleRewardBasedVideoRewardedOnAdRewarded。所以,第一次它会调用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 ();
        }
    }
}
于 2017-10-25T18:33:40.377 回答
0

我解决了我的问题!我认为它的小菜鸟有点它的工作......: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);
                    }
                }
            }
         }
      }
于 2017-10-27T15:14:17.753 回答