Unity3d Admob rewarded video ads

我的未来我决定 提交于 2019-12-23 04:32:29

问题


I'm having troubles showing the rewarded video ad from admob plugin in Unity3d. First of all this is my script:

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);
    }
}

I run the RequestRewardVideo() Method with a button, but nothing happens. I tried to run the requestrewardvideo() method in the start() and showAdd() form the button but only shows the video 1/3 times... whats wrong with this?

Thanks in advance


回答1:


I run the RequestRewardVideo() Method with a button, but nothing happens.

You must maintain some time difference between load and show Ad because Ad takes some time in loading. when you call in showAd just After loading rewardBasedVideo.IsLoaded() return false.

So call requestrewardvideo() from start() and on button Click call 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);
        }
}

Also Ad A listener When Ad Close and request to loadAd again there. So that each time rewardBasedVideo.IsLoaded() return true.

EDIT

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

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



回答2:


if you are using this plugin https://github.com/unity-plugins/Unity-Admob then code will like this.

load video

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

Shown at game over , check that the video is ready before showing it:

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

handle ad event

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);
    }

}


来源:https://stackoverflow.com/questions/43891119/unity3d-admob-rewarded-video-ads

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!