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

时间:2018-06-25 16:23:53

标签: javascript unity3d admob admob-rewardedvideoad

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

public class admobVideo : MonoBehaviour {

	RewardBasedVideoAd rewardBasedVideo;

	static InterstitialAd interstitial;

	string VideoID = "ca-app-pub-6032262586397129~2821965113";
	string adUnitId = "ca-app-pub-6032262586397129/5003220953";

	public static admobVideo 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("coins", PlayerPrefs.GetInt("coins") + 5);
		GameObject.FindGameObjectWithTag("Coins").GetComponent<Text>().text = PlayerPrefs.GetInt("coins").ToString();
		GameObject.FindGameObjectWithTag("Double").GetComponent<Button>().interactable = false;

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

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

我用5个硬币奖励玩家,但是当我单击按钮时,什么都没有出现,我试图以多种方式更改代码,但没有取得积极的结果。

当我统一单击按钮时,控制台会显示“ Dummy已加载”和“ Dummy showrewardedbasedvideoad”

单击按钮时调用的方法是ShowAd()。请帮助

1 个答案:

答案 0 :(得分:0)

  1. 请通过在HandleRewardBasedVideoRewarded方法中添加调试来检查是否被调用。

  2. 还请检查您是否已为它添加了侦听器,因为您在上面提到的代码中没有提到此内容。

    rewardBasedVideo.OnAdRewarded + = this.HandleRewardBasedVideoRewarded;

  3. 您尚未使用应用ID

    初始化mobileAds

    MobileAds.Initialize();