1

我正在尝试将 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)
  {

  }
}
4

2 回答 2

0

初始化 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 插件

于 2017-07-15T06:41:48.970 回答
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();
    }
 }
于 2017-04-07T17:07:56.063 回答