如何在Unity游戏中展示admob interstitial和奖励广告?

时间:2017-02-26 21:31:14

标签: unity3d mobile admob ads

当游戏结束或暂停时,任何人都可以给我c#脚本来显示插页式广告。我已经尝试过官方文档中的代码,但它没有工作,所以如果有人知道如何做,所以给我脚本。

1 个答案:

答案 0 :(得分:2)

未经测试但应该有效:

编辑:编辑过的部分实际上来自谷歌的教程。 有时谷歌,你会学到很多东西。

using System;
using UnityEngine;
using GoogleMobileAds.Api;

public class AdMobOnPause : MonoBehaviour
{
    public string AndroidInterstitialID = "INSERT_ANDROID_INTERSTITIAL_AD_UNIT_ID_HERE";
public string IOSInterstitialID = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";


private static AdMobOnPause instance;

private InterstitialAd interstitial;
// ADD THIS
private RewardBasedVideoAd rewardBasedVideo;

private AdRequest request;
void Awake()
{
    if (AdMobOnPause.instance != null)
    {
        Destroy(gameObject);    //just allow one adscontroller on scene over gameplay, even when you restart this level
    }
}
void Start()
{
    DontDestroyOnLoad(gameObject);
    instance = this;
    RequestInterstitial();
    //Request Video!
    RequestRewardBasedVideo();
}

//ADD THESE TWO METHODS
void OnEnable() {
rewardBasedVideo.OnAdRewarded += HandleRewardedSuccessful;
}

void OnDisable() {
rewardBasedVideo.OnAdRewarded -= HandleRewardedSuccessful;
}

private void RequestInterstitial()
{
#if UNITY_ANDROID
    string adUnitId = AndroidInterstitialID;
#elif UNITY_IPHONE
    string adUnitId = IOSInterstitialID;
#else
    string adUnitId = "unexpected_platform";
#endif

    // Initialize an InterstitialAd.
    interstitial = new InterstitialAd(adUnitId);
    interstitial.OnAdClosed += HandleEventHandler;
    // Create an empty ad request.
    request = new AdRequest.Builder().Build();
    // Load the interstitial with the request.
    interstitial.LoadAd(request);
}

void HandleEventHandler(object sender, System.EventArgs e)
{
    interstitial.Destroy();
    interstitial.LoadAd(request);
}

public void Pause()
{
    //your pause code here or,
    //if(Time.timeScale < 1)
    //    Time.timeScale = 1;
    //else
    //    Time.timeScale = 0;
    if (interstitial.IsLoaded())
    {
        interstitial.Show();
    }
}
//From here on is the Rewarded video code.
private void RequestRewardBasedVideo()
{
#if UNITY_ANDROID
    string adUnitId = "INSERT_AD_UNIT_HERE";
#elif UNITY_IPHONE
    string adUnitId = "INSERT_AD_UNIT_HERE";
#else
    string adUnitId = "unexpected_platform";
#endif

    rewardBasedVideo = RewardBasedVideoAd.Instance;

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

public void GameOver()
{
    if (rewardBasedVideo.IsLoaded())
    {
        rewardBasedVideo.Show();
    }
}

public void HandleRewardedSuccessful() {
// Do Something when rewarded video is successfully watched
}

}

相关问题