【问题标题】:Unity: Admob Reward Video Ad Doesn't Call EventsUnity:Admob 奖励视频广告不调用事件
【发布时间】:2017-04-07 16:46:07
【问题描述】:

我正在尝试将 Admob 奖励视频广告添加到我用 Unity 制作的 Android 游戏中。显示很好,但是当我关闭广告时,奖励永远不会给出。我已经测试了函数中的代码并且工作正常,所以我认为问题在于没有被调用。谁能帮帮我?

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)
  {

  }
}

【问题讨论】:

    标签: android unity3d admob rewardedvideoad


    【解决方案1】:

    如果您致电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();
        }
     }
    

    【讨论】:

    • watched 变量一开始是空的,并且仅在 HandleOnAdRewarded 上设置为 true,因此当用户在观看完广告后关闭广告时,他们将获得另一个生命的奖励。但问题是,这些 HandleOnAd.... 函数没有被调用。
    【解决方案2】:

    初始化 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
        }
    }
    

    参考admob plugin

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-19
      • 1970-01-01
      相关资源
      最近更新 更多