unity 接入谷歌广告
程序员文章站
2022-07-13 16:39:08
...
官方文档:https://developers.google.cn/admob/unity/quick-start
使用实例:
using UnityEngine;
using GoogleMobileAds.Api;
using UnityEngine.UI;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public string adUnitId = "ca-app-pub-3940256099942544/6300978111";
public Button openBannerButton;
public Button openInterstitialButton;
public Button openRewardedButton;
public Button destroyButton;
Ad.AdObject adObject;
void Start()
{
Ad.AdSetting.adUnitId = adUnitId;
MobileAds.Initialize(initStatus => { });
openBannerButton.onClick.AddListener(OpenBanner);
openInterstitialButton.onClick.AddListener(OpenInterstitial);
openRewardedButton.onClick.AddListener(OpenRearded);
destroyButton.onClick.AddListener(OnDestroy);
}
void OpenBanner()
{
var banner = Ad.AdFactory.CreateBanner();
banner.Load();
adObject = banner;
}
void OpenInterstitial()
{
var Interstitial = Ad.AdFactory.CreateInterstitial();
Interstitial.Load();
adObject = Interstitial;
}
void OpenRearded()
{
var rewrded = Ad.AdFactory.CreateRewarded();
rewrded.OnUserEarnedReward = (s, r) => {
Debug.Log("获得奖励");
};
rewrded.Load();
adObject = rewrded;
}
private void OnDestroy()
{
if (adObject == null)
return;
adObject.Dispose();
adObject = null;
}
}
广告工厂:
namespace Ad
{
public static class AdFactory
{
/// <summary>
/// 横幅
/// </summary>
/// <returns></returns>
public static Banner CreateBanner()
{
return new Banner();
}
/// <summary>
/// 插页式
/// </summary>
/// <returns></returns>
public static Interstitial CreateInterstitial()
{
return new Interstitial();
}
/// <summary>
/// 激励
/// </summary>
/// <returns></returns>
public static Rewarded CreateRewarded()
{
return new Rewarded();
}
}
}
Id配置:
namespace Ad
{
public static class AdSetting
{
#if UNITY_ANDROID
public static string adUnitId= "ca-app-pub-3940256099942544/6300978111";
#elif UNITY_IPHONE
public static string adUnitId="ca-app-pub-3940256099942544/6300978111";
#else
public static string adUnitId="ca-app-pub-3940256099942544/6300978111";
#endif
}
}
横幅广告:
using System;
using GoogleMobileAds.Api;
namespace Ad
{
public class Banner:AdObject
{
public AdSize adSize= AdSize.SmartBanner;
public AdPosition position = AdPosition.Bottom;
/// <summary>
/// 广告加载失败时
/// </summary>
public EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;
/// <summary>
/// 用户点击打开其他应用
/// </summary>
public EventHandler<EventArgs> OnAdLeavingApplication;
BannerView bannerView;
protected override void OnLoad()
{
bannerView = new BannerView(AdSetting.adUnitId, adSize, position);
bannerView.OnAdLoaded += OnAdLoaded;
bannerView.OnAdFailedToLoad += OnAdFailedToLoad;
bannerView.OnAdOpening += OnAdOpening;
bannerView.OnAdClosed += OnAdClosed;
bannerView.OnAdLeavingApplication += OnAdLeavingApplication;
bannerView.LoadAd(CreateAdRequest());
}
public override void Dispose()
{
if (bannerView == null)
return;
bannerView.Destroy();
bannerView = null;
}
}
}
插页式广告:
using System;
using GoogleMobileAds.Api;
namespace Ad
{
public class Interstitial : AdObject
{
/// <summary>
/// 广告加载失败时
/// </summary>
public EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;
/// <summary>
/// 用户点击打开其他应用
/// </summary>
public EventHandler<EventArgs> OnAdLeavingApplication;
InterstitialAd interstitial;
protected override void OnLoad()
{
interstitial = new InterstitialAd(AdSetting.adUnitId);
interstitial.OnAdLoaded += OnAdLoaded;
interstitial.OnAdFailedToLoad += OnAdFailedToLoad;
interstitial.OnAdOpening += OnAdOpening;
interstitial.OnAdClosed += OnAdClosed;
interstitial.OnAdLeavingApplication += OnAdLeavingApplication;
interstitial.LoadAd(CreateAdRequest());
if (interstitial.IsLoaded())
{
interstitial.Show();
}
}
public override void Dispose()
{
if (interstitial == null)
return;
interstitial.Destroy();
interstitial = null;
}
}
}
激励广告:
using System;
using GoogleMobileAds.Api;
namespace Ad
{
public class Rewarded : AdObject
{
/// <summary>
/// 广告加载失败时
/// </summary>
public EventHandler<AdErrorEventArgs> OnAdFailedToLoad;
/// <summary>
/// 在广告显示失败时被调用
/// </summary>
public EventHandler<AdErrorEventArgs> OnAdFailedToShow;
/// <summary>
/// 在用户因观看视频而应获得奖励时被调用
/// </summary>
public EventHandler<Reward> OnUserEarnedReward;
RewardedAd rewarded;
protected override void OnLoad()
{
rewarded = new RewardedAd(AdSetting.adUnitId);
rewarded.OnAdLoaded += OnAdLoaded;
rewarded.OnAdOpening += OnAdOpening;
rewarded.OnAdClosed += OnAdClosed;
rewarded.OnAdFailedToLoad += OnAdFailedToLoad;
rewarded.OnAdFailedToShow += OnAdFailedToShow;
rewarded.OnUserEarnedReward += OnUserEarnedReward;
rewarded.LoadAd(CreateAdRequest());
if (rewarded.IsLoaded())
{
rewarded.Show();
}
}
public override void Dispose()
{
rewarded = null;
}
}
}
上一篇: 江天一色无纤尘————管道
下一篇: MyBatis3实现