设置完Google广告之后导入项目
调用方式:
AdsManager.Main.ShowBanner(); //显示横屏广告
AdsManager.Main.ShowInterstitial(); //显示插屏广告
AdsManager.Main.ShowWatchVideo(); //播放激励视频
using System;
using UnityEngine;
using GoogleMobileAds.Api;
public class AdsManager : MonoBehaviour
{
private bool noads
{
get
{
if (PlayerPrefs.GetInt("Ads") == 0)
{
return false;
}
else
{
return true;
}
}
set
{
if (value)
{
PlayerPrefs.SetInt("Ads",1);
}
else
{
PlayerPrefs.SetInt("Ads",0);
}
PlayerPrefs.Save();
}
}
//是否开启广告
public bool Noads
{
get { return noads; }
set
{
if (!value)
{
HideBanner();
}
noads = value;
}
}
private static AdsManager main;
public static AdsManager Main
{
get { return main; }
}
private void Awake()
{
main = this;
DontDestroyOnLoad(gameObject);
if (Application.platform == RuntimePlatform.Android)
{
// bannerID = "安卓横屏广告ID";
// interstitialID = "安卓插屏广告ID";
// watchVedioID = "安卓激励视频广告ID";
// 以下为对应的测试ID
bannerID = "ca-app-pub-3940256099942544/6300978111";
interstitialID = "ca-app-pub-3940256099942544/1033173712";
watchVedioID = "ca-app-pub-3940256099942544/5224354917";
}
else if (Application.platform == RuntimePlatform.IPhonePlayer)
{
// bannerID = "苹果横屏广告ID";
// interstitialID = "苹果插屏广告ID";
// watchVedioID = "苹果激励视频广告ID";
// 以下为对应的测试ID
bannerID = "ca-app-pub-3940256099942544/2934735716";
interstitialID = "ca-app-pub-3940256099942544/4411468910";
watchVedioID = "ca-app-pub-3940256099942544/1712485313";
}
ShowBanner();
LoadInterstitial();
rewardBasedVideo.OnAdRewarded += (sender, reward) => { watchVedioClose();};
rewardBasedVideo.OnAdClosed += (sender, reward) => {LoadWatchVedio();};
interstitial.OnAdClosed += (sender, args) => { adsClose();
interstitial.Destroy(); LoadInterstitial();};
}
private string bannerID = "";
private string interstitialID = "";
private string watchVedioID = "";
private BannerView bannerView;
private InterstitialAd interstitial;
private RewardBasedVideoAd rewardBasedVideo;
public Action adsClose;
public Action watchVedioClose;
private void ShowBanner()
{
if (!noads)
{
bannerView = new BannerView(bannerID, AdSize.SmartBanner, AdPosition.Bottom);
AdRequest request = new AdRequest.Builder().Build();
this.bannerView.LoadAd(request);
}
}
private void HideBanner()
{
if (bannerView != null) bannerView.Destroy();
}
private void LoadInterstitial()
{
interstitial = new InterstitialAd(interstitialID);
AdRequest request = new AdRequest.Builder().Build();
interstitial.LoadAd(request);
}
public void ShowInterstitial(Action action)
{
adsClose = action;
if (!noads)
{
if (interstitial.IsLoaded())
{
interstitial.Show();
}
else
{
adsClose();
LoadInterstitial();
}
}
else
{
adsClose();
}
}
private void LoadWatchVedio()
{
rewardBasedVideo = RewardBasedVideoAd.Instance;
AdRequest request = new AdRequest.Builder().Build();
rewardBasedVideo.LoadAd(request, watchVedioID);
}
public void ShowWatchVideo(Action action)
{
watchVedioClose = action;
if (interstitial.IsLoaded())
{
rewardBasedVideo.Show();
}
else
{
LoadWatchVedio();
}
}
}