Unity:Admob奖励视频广告不会调用事件

时间:2017-04-07 16:46:07

标签: android unity3d admob rewardedvideoad

我正在尝试将Admob奖励视频广告添加到我在Unity制作的Android游戏中。显示效果很好但是当我关闭广告时,奖励从未给出。我已经测试了函数中的代码,并且工作正常,所以我认为问题是没有调用gettting。任何人都可以帮助我吗?

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

public class textEdit : MonoBehaviour
{
   public Image lifeAdUI;
   static Image lifeAdUIStat;
   public Text adFailUI;
   static Text adFailUIStat;
   public Button lifeButton;

   private static RewardBasedVideoAd videoAd;
   static bool adTime = false;
   static bool adPlaying = false;
   static int pass = 0;
   bool watched;

  // Use this for initialisation
  void Start()
  {
    Button btn = lifeButton.GetComponent<Button>();
    btn.onClick.AddListener(VideoAd);

    videoAd = RewardBasedVideoAd.Instance;

    videoAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
    videoAd.OnAdOpening += HandleOnAdOpening;
    videoAd.OnAdClosed += HandleOnAdClosed;
    videoAd.OnAdRewarded += HandleOnAdReward;
    videoAd.OnAdLeavingApplication += HandleOnAdLeavingApplication;
    videoAd.OnAdLoaded += HandleOnAdLoaded;
    videoAd.OnAdStarted += HandleOnAdStarted;

    lifeAdUIStat = lifeAdUI;
    adFailUIStat = adFailUI;

  }


public static void LoadVideoAd()
{
#if UNITY_EDITOR
    string adUnitID = "unused";
#elif UNITY_ANDROID
    string adUnitID = "ca-app-pub-3025391748532285/9122766975";
#elif UNITY_IPHONE
    string adUnitID = "";
#else
    string adUnitID = "unexpected_platform";
#endif


    videoAd.LoadAd(new AdRequest.Builder().Build(), adUnitID);
    pass = pass + 1;

}

void VideoAd()
{
    if (videoAd.IsLoaded())
    {
        videoAd.Show();


    }
    else
    {
        //ad not loaded
    }
}

//Ad Events
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
    if (pass < 2)
    {
        LoadVideoAd();
    }
    else
    {
        StartCoroutine(adFailCoro());
    }
}

 public void HandleOnAdOpening(object ssender, EventArgs args)
 {
    adPlaying = true;
 }

 public void HandleOnAdClosed(object sender, EventArgs args)
 {
    adPlaying = false;
    watched = true;

    if (watched == true)
    {
        control controlScript = GameObject.FindGameObjectWithTag("Control").GetComponent<control>();

        lifeAdUI.enabled = false;
        StartCoroutine(controlScript.ExtraLife());
    }
 }

 public void HandleOnAdReward(object sender, EventArgs args)
 {
    watched = true;
 }

 public void HandleOnAdLeavingApplication(object sender, EventArgs args)
 {


 }

  public void HandleOnAdLoaded(object sender, EventArgs args)
  {

  }

  public void HandleOnAdStarted(object sender, EventArgs args)
  {

  }
}

2 个答案:

答案 0 :(得分:0)

如果您致电VideoAd来展示您的视频广告,但由于某些无法预料的原因您的视频尚未加载或加载失败,请求再次加载广告。

void VideoAd()
{
    if (videoAd.IsLoaded())
    {
        videoAd.Show();
    }
    else
    {
        LoadVideoAd();
    }
}

请求在用户关闭广告时加载新的视频广告。

public void HandleOnAdClosed(object sender, EventArgs args)
{
    adPlaying = false;
    watched = true;

    if (watched == true)  //what is the need of this condition, it always true 
    {
        control controlScript = GameObject.FindGameObjectWithTag("Control").GetComponent<control>();

        lifeAdUI.enabled = false;
        StartCoroutine(controlScript.ExtraLife());
        LoadVideoAd();
    }
 }

答案 1 :(得分:0)

Init Admob Unity插件

using admob;
    Admob.Instance().initAdmob("admob banner id", "admob interstitial id");//admob id with format ca-app-pub-279xxxxxxxx/xxxxxxxx
    //Admob.Instance().initAdmob("ca-app-pub-3940256099942544/2934735716", "ca-app-pub-3940256099942544/4411468910");

以下是创建admob视频的最小代码。

Admob.Instance().loadRewardedVideo("ca-app-pub-3940256099942544/1712485313"); 

视频需要在应用中的适当停留点明确显示,在显示之前检查视频是否已就绪:

if (Admob.Instance().isRewardedVideoReady()) {
  Admob.Instance().showRewardedVideo();
}
处理奖励

Admob.Instance().videoEventHandler += onVideoEvent;
void onVideoEvent(string eventName, string msg)
{
    Debug.Log("handler onAdmobEvent---" + eventName + "   " + msg);
    if (eventName == AdmobEvent.onRewarded)
    {
        //msg is the reward count.you can handle it now
    }
}

ref admob plugin