Unity3d Admob奖励视频广告

时间:2017-05-10 11:26:33

标签: c# unity3d admob

我很难在Unity3d中显示来自admob插件的奖励视频广告。 首先,这是我的剧本:

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

public class AdManager : MonoBehaviour {
    private RewardBasedVideoAd rewardBasedVideo;

    // Use this for initialization
    void Start () {

    }

    // Update is called once per frame
    void Update () {

    }

    public void RequestRewardBasedVideo()
    {
#if UNITY_EDITOR
        string adUnitId = "unused";
#elif UNITY_ANDROID
        string adUnitId = "INSERT_AD_UNIT_HERE";
#elif UNITY_IPHONE
        string adUnitId = "INSERT_AD_UNIT_HERE";
#else
        string adUnitId = "unexpected_platform";
#endif

        RewardBasedVideoAd rewardBasedVideo = RewardBasedVideoAd.Instance;

        AdRequest request = new AdRequest.Builder().Build();
        rewardBasedVideo.LoadAd(request, adUnitId);

        //Show Ad
        showAdd(rewardBasedVideo);
    }

    private void showAdd(RewardBasedVideoAd rewardBasedVideo)
    {
        if (rewardBasedVideo.IsLoaded())
        {
            //Subscribe to Ad event
            rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
            rewardBasedVideo.Show();
        }
    }

    private void HandleRewardBasedVideoRewarded(object sender, Reward e)
    {
        Debug.Log("30 monedas ;)");
        Debug.Log(e);
    }
}

我使用按钮运行RequestRewardVideo()方法,但没有任何反应。我尝试在requestrewardvideo()中运行start()方法并在showAdd()形成按钮,但只显示视频1/3次...这有什么不对吗?

提前致谢

2 个答案:

答案 0 :(得分:0)

我使用按钮运行RequestRewardVideo()方法,但没有任何反应。

您必须在加载和展示广告之间保持一些时差,因为广告需要一些时间加载。当您在showAd中调用时,加载rewardBasedVideo.IsLoaded()后返回false。

requestrewardvideo()拨打start()并点击按钮点击通话showAdd()

private void showAdd(RewardBasedVideoAd rewardBasedVideo) {
        if (rewardBasedVideo.IsLoaded())
        {
            //Subscribe to Ad event
            rewardBasedVideo.OnAdRewarded += HandleRewardBasedVideoRewarded;
            rewardBasedVideo.Show();
        }else {
            AdRequest request = new AdRequest.Builder().Build();
            rewardBasedVideo.LoadAd(request, adUnitId);
        }
}

另外广告A听众当广告关闭并再次请求加载广告时。这样每次rewardBasedVideo.IsLoaded()都返回true。

修改

rewardBasedVideo.OnAdClosed += HandleOnAdClosed;  //Ad listener to your rewardBasedVideo

public void HandleOnAdClosed(object sender, EventArgs args)
{ 
    //   Load it here
}

答案 1 :(得分:0)

如果您使用此插件https://github.com/unity-plugins/Unity-Admob 然后代码会喜欢这个。

加载视频

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

在游戏结束时显示,在显示视频之前检查视频是否准备就绪:

   if (Admob.Instance().isRewardedVideoReady()) {
     Admob.Instance().showRewardedVideo();
   }

处理广告事件

Admob.Instance().videoEventHandler += onInterstitialEvent;
    void onInterstitialEvent(string eventName, string msg)
    {
    Debug.Log("handler onAdmobEvent---" + eventName + "   " + msg);
    if (eventName == AdmobEvent.onAdLoaded)
    {
        Admob.Instance().showRewardedVideo();
    }
    if(eventName==AdmobEvent.onAdFail){
    Debug.log(msg);
    }

}