如何在 Unity 游戏中显示 admob 插屏和奖励广告?

How to show admob interstitial and reward ads in Unity game?

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

未测试但应该有效:

编辑:编辑的部分实际上来自google的教程。 偶尔做google,你会学到很多东西。

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
}

}