Skip to main content

整合的广告代码

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

public class AdMob : MonoBehaviour
{

//横幅广告 ca-app-pub-3940256099942544/6300978111
//插页式广告 ca-app-pub-3940256099942544/1033173712
//激励广告 ca-app-pub-3940256099942544/5224354917
//插页式激励广告 ca-app-pub-3940256099942544/5354046379
//原生广告 ca-app-pub-3940256099942544/2247696110

// 这是测试的广告
protected string BannerAdUnitId = "ca-app-pub-3940256099942544/6300978111";
protected string InterstitialAdUnitId = "ca-app-pub-3940256099942544/1033173712";
protected string RewardedAdUnitId = "ca-app-pub-3940256099942544/5224354917";
protected string RewardedInterstitialAdUnitId = "ca-app-pub-3940256099942544/5354046379";

string adUnitId;

//// 这是真实的广告
//protected string BannerAdUnitId = "ca-app-pub-8064899581399055/7059612657";
//protected string InterstitialAdUnitId = "ca-app-pub-8064899581399055/1963833108";
//protected string RewardedAdUnitId = "ca-app-pub-8064899581399055/6792019349";
//protected string RewardedInterstitialAdUnitId = "ca-app-pub-8064899581399055/9257003727";


private BannerView bannerView;
private InterstitialAd interstitial;
private RewardedAd rewardedAd;
private RewardedInterstitialAd rewardedInterstitialAd;

// Start is called before the first frame update
public void Start()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(initStatus => {
this.RequestBanner(); // 横幅广告
this.RequestInterstitial(); // 插页广告
this.RequestRewarded(); // 激励广告
this.RequestRewardedInterstitialAd(); //插页式激励广告

ShowRewardedInterstitialAd();
});
}



/******************调用广告的方法*****************************/
// 横幅广告
private void ShowBannerAd()
{
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the banner with the request.
this.bannerView.LoadAd(request);
}

// 插页广告
private void ShowInterstitialAd()
{
if (this.interstitial.IsLoaded())
{
this.interstitial.Show();
}
}

// 激励广告
private void ShowRewardedAd()
{
if (this.rewardedAd.IsLoaded())
{
this.rewardedAd.Show();
}
}

// 插页式激励广告
public void ShowRewardedInterstitialAd()
{
if (rewardedInterstitialAd != null)
{
rewardedInterstitialAd.Show(userEarnedRewardCallback);
}
}

private void userEarnedRewardCallback(Reward reward)
{
// TODO: Reward the user.
}

/*************************** 横幅广告 start*************************************/

private void RequestBanner()
{

#if UNITY_ANDROID
adUnitId = BannerAdUnitId;
#elif UNITY_IPHONE
adUnitId = "ca-app-pub-3940256099942544/2934735716";
#else
adUnitId = "unexpected_platform";
#endif

this.bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);

// Called when an ad request has successfully loaded.
this.bannerView.OnAdLoaded += this.HandleOnBannerAdLoaded;
// Called when an ad request failed to load.
this.bannerView.OnAdFailedToLoad += this.HandleOnBannerAdFailedToLoad;
// Called when an ad is clicked.
this.bannerView.OnAdOpening += this.HandleOnBannerAdOpened;
// Called when the user returned from the app after an ad click.
this.bannerView.OnAdClosed += this.HandleOnBannerAdClosed;
// Called when the ad click caused the user to leave the application.
this.bannerView.OnAdLeavingApplication += this.HandleOnBannerAdLeavingApplication;


}

public void HandleOnBannerAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleBannerAdLoaded event received");
}

public void HandleOnBannerAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleBannerFailedToReceiveAd event received with message: "
+ args.Message);
}

public void HandleOnBannerAdOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleBannerAdOpened event received");
}

public void HandleOnBannerAdClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleBannerAdClosed event received");
}

public void HandleOnBannerAdLeavingApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleBannerAdLeavingApplication event received");
}

/*************************** 横幅广告 end*************************************/

/*************************** 插页广告 start*************************************/
private void RequestInterstitial()
{
#if UNITY_ANDROID
adUnitId = InterstitialAdUnitId;
#elif UNITY_IPHONE
adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
adUnitId = "unexpected_platform";
#endif

// Initialize an InterstitialAd.
this.interstitial = new InterstitialAd(adUnitId);

// Called when an ad request has successfully loaded.
this.interstitial.OnAdLoaded += HandleOnInterstitialAdLoaded;
// Called when an ad request failed to load.
this.interstitial.OnAdFailedToLoad += HandleOnInterstitialAdFailedToLoad;
// Called when an ad is shown.
this.interstitial.OnAdOpening += HandleOnInterstitialAdOpened;
// Called when the ad is closed.
this.interstitial.OnAdClosed += HandleOnInterstitialAdClosed;
// Called when the ad click caused the user to leave the application.
this.interstitial.OnAdLeavingApplication += HandleOnInterstitialAdLeavingApplication;

// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
this.interstitial.LoadAd(request);
}

public void HandleOnInterstitialAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialAdLoaded event received");
}

public void HandleOnInterstitialAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleInterstitialFailedToReceiveAd event received with message: "
+ args.Message);
}

public void HandleOnInterstitialAdOpened(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialAdOpened event received");
}

public void HandleOnInterstitialAdClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialAdClosed event received");
}

public void HandleOnInterstitialAdLeavingApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialAdLeavingApplication event received");
}

/*************************** 插页广告 end*************************************/

/*************************** 激励广告 start***********************************/
public void RequestRewarded()
{
#if UNITY_ANDROID
adUnitId = RewardedAdUnitId;
#elif UNITY_IPHONE
adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
adUnitId = "unexpected_platform";
#endif

this.rewardedAd = new RewardedAd(adUnitId);

// Called when an ad request has successfully loaded.
this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
// Called when an ad request failed to load.
this.rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad;
// Called when an ad is shown.
this.rewardedAd.OnAdOpening += HandleRewardedAdOpening;
// Called when an ad request failed to show.
this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
// Called when the user should be rewarded for interacting with the ad.
this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
// Called when the ad is closed.
this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;

// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded ad with the request.
this.rewardedAd.LoadAd(request);
}

public void HandleRewardedAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardedAdLoaded event received");
}

public void HandleRewardedAdFailedToLoad(object sender, AdErrorEventArgs args)
{
MonoBehaviour.print(
"HandleRewardedAdFailedToLoad event received with message: "
+ args.Message);
}

public void HandleRewardedAdOpening(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardedAdOpening event received");
}

public void HandleRewardedAdFailedToShow(object sender, AdErrorEventArgs args)
{
MonoBehaviour.print(
"HandleRewardedAdFailedToShow event received with message: "
+ args.Message);
}

public void HandleRewardedAdClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardedAdClosed event received");
}

public void HandleUserEarnedReward(object sender, Reward args)
{
string type = args.Type;
double amount = args.Amount;
MonoBehaviour.print(
"HandleRewardedAdRewarded event received for "
+ amount.ToString() + " " + type);
}

/*************************** 激励广告 end*************************************/

/*************************** 插页式激励广告 start*************************************/

public void RequestRewardedInterstitialAd()
{
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded ad with the request.
RewardedInterstitialAd.LoadAd(adUnitId, request, adLoadCallback);
}


private void adLoadCallback(RewardedInterstitialAd ad, string error)
{
if (error == null)
{
rewardedInterstitialAd = ad;

rewardedInterstitialAd.OnAdFailedToPresentFullScreenContent += HandleRewardedInterstitialAdFailedToPresent;
rewardedInterstitialAd.OnAdDidPresentFullScreenContent += HandleRewardedInterstitialAdDidPresent;
rewardedInterstitialAd.OnAdDidDismissFullScreenContent += HandleRewardedInterstitialAdDidDismiss;
rewardedInterstitialAd.OnPaidEvent += HandlePaidEvent;
}
}

private void HandleRewardedInterstitialAdFailedToPresent(object sender, AdErrorEventArgs args)
{
MonoBehaviour.print("Rewarded interstitial ad has failed to present.");
}

private void HandleRewardedInterstitialAdDidPresent(object sender, EventArgs args)
{
MonoBehaviour.print("Rewarded interstitial ad has presented.");
}

private void HandleRewardedInterstitialAdDidDismiss(object sender, EventArgs args)
{
MonoBehaviour.print("Rewarded interstitial ad has dismissed presentation.");
}

private void HandlePaidEvent(object sender, AdValueEventArgs args)
{
MonoBehaviour.print(
"Rewarded interstitial ad has received a paid event.");
}
/*************************** 插页式激励广告 end*************************************/

}