简体   繁体   中英

How to properly reward player with Admob rewarded video in Unity?

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

public class AdManager : MonoBehaviour {

RewardBasedVideoAd rewardBasedVideo;

static InterstitialAd interstitial;

string VideoID = "ca-app-pub-3888784411212422/2850896103";
string adUnitId = "ca-app-pub-3888784411212422/4158795262";

public static AdManager Instance;
void Start ()
{
    Instance = this;
    DontDestroyOnLoad(gameObject);
    RequestRewardBasedVideo();
    RequestInterstitial();
}

public void RequestRewardBasedVideo()
{       
    rewardBasedVideo = RewardBasedVideoAd.Instance;
    rewardBasedVideo.LoadAd(new AdRequest.Builder().Build(), adUnitId);
}

public void RequestInterstitial()
{
    interstitial = new InterstitialAd(VideoID);
    interstitial.LoadAd(new AdRequest.Builder().Build());
}
public void ShowAd()
{
    if(rewardBasedVideo.IsLoaded())
    {
        rewardBasedVideo.Show();
        rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
    }
}
public static void ShowInter()
{
    showInterstitial(interstitial);
}


private void showAdd(RewardBasedVideoAd r)
{
    if (r.IsLoaded())
    {
        //Subscribe to Ad event
        r.Show();
        r.OnAdRewarded += HandleRewardBasedVideoRewarded;
    }
}

public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{

    PlayerPrefs.SetInt("coin", PlayerPrefs.GetInt("coin") + 200);
    GameObject.FindGameObjectWithTag("Coin").GetComponent<Text>().text = PlayerPrefs.GetInt("coin").ToString();
    GameObject.FindGameObjectWithTag("Double").GetComponent<Button>().interactable = false;

    Debug.Log("Pref: " + PlayerPrefs.GetInt("coin"));
}

static void showInterstitial(InterstitialAd i)
{
    if (i.IsLoaded())
    {
        i.Show();
    }
}
}

I am rewarding players with 200 coins, but somehow every time i get reward increased by 200. First time when player get rewarded he gets 200, next time he gets 400, next time 600 etc. I have tried to change code in many ways but no positive result.

Method that is called upon button click is ShowAd(). Every time when panel with button thats calls the method is shown i call RequestRewardBasedVideo().

adding and creating new objects is the answer, for example the Void would have something like that

 public void HandleRewardBasedVideoRewarded(object sender, Reward args)
    {
        int CoinOld = PlayerPrefs.GetInt("coin");
        CoinOld += 200;
        PlayerPrefs.SetInt("coin", CoinOld);
        GameObject.FindGameObjectWithTag("Coin").GetComponent<Text>().text = PlayerPrefs.GetInt("coin").ToString();
        GameObject.FindGameObjectWithTag("Double").GetComponent<Button>().interactable = false;

        Debug.Log("Pref: " + PlayerPrefs.GetInt("coin"));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM