繁体   English   中英

如何在Unity中使用Admob奖励视频正确奖励玩家?

[英]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();
    }
}
}

我用200个硬币奖励玩家,但是我每次获得奖励的方式都会增加200。第一次奖励玩家时,他奖励200,下次他奖励400,下次奖励600,等等。我试图通过多种方式更改代码,但是没有积极的结果。

单击按钮时调用的方法是ShowAd()。 每次当带按钮的面板调用该方法时,我都会调用RequestRewardBasedVideo()。

答案就是添加和创建新对象,例如Void会有类似的东西

 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"));
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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