在 Unity 上从 Google AdMob (ex. AdMob) 迁移到 Yandex 聚合
要在 Unity 上使用 Yandex Ads 聚合替换 Google AdMob (ex. AdMob) 广告,请对您的代码进行以下更改。
集成SDK
库通过 Unity Play Services Resolver 库 进行分发。将以下插件资源添加到项目中:
Google AdMob (ex. AdMob)
Yandex Ads
选择 Assets → Import Package → Custom Package,然后找到导入的 GoogleMobileAdsPlugin.unitypackage
文件。
选择 Assets → Import Package → Custom Package,然后找到导入的 yandex-mobileads-lite-2.9.0.unitypackage
文件。从 mobileads-mediation
目录中导入 mobileads-admob-mediation-2.9.0.unitypackage
。
在加载广告之前,初始化库:
Google AdMob (ex. AdMob)
Yandex Ads
...
using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public void Start()
{
// 初始化 Google Mobile Ads SDK。
MobileAds.Initialize(initStatus => { });
}
}
// 非必要
广告格式
插屏广告
创建广告对象
Google AdMob (ex. AdMob)
Yandex Ads
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
...
private InterstitialAd interstitial;
private void RequestInterstitial()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
string adUnitId = "unexpected_platform";
#endif
this.interstitial = new InterstitialAd(adUnitId);
}
using YandexMobileAds;
using YandexMobileAds.Base;
...
private Interstitial interstitial;
private void RequestInterstitial()
{
#if UNITY_ANDROID
string adUnitId = "demo-interstitial-yandex";
#elif UNITY_IPHONE
string adUnitId = "demo-interstitial-yandex";
#else
string adUnitId = "unexpected_platform";
#endif
this.interstitial = new Interstitial(adUnitId);
}
加载广告
Google AdMob (ex. AdMob)
Yandex Ads
// 创建一个空的广告请求。
AdRequest request = new AdRequest.Builder().Build();
// 使用请求加载插屏广告。
this.interstitial.LoadAd(request);
// 创建一个空的广告请求。
AdRequest request = new AdRequest.Builder().Build();
// 使用请求加载插屏广告。
this.interstitial.LoadAd(request);
展示广告
Google AdMob (ex. AdMob)
Yandex Ads
private void ShowInterstitial()
{
if (this.interstitial.IsLoaded()) {
this.interstitial.Show();
} else
{
Debug.Log("Interstitial is not ready yet");
}
}
private void ShowInterstitial()
{
if (this.interstitial.IsLoaded()) {
interstitial.Show();
} else
{
Debug.Log("Interstitial is not ready yet");
}
}
清除广告
Google AdMob (ex. AdMob)
Yandex Ads
interstitial.Destroy();
interstitial.Destroy();
设置广告回调
Google AdMob (ex. AdMob)
Yandex Ads
...
private void RequestInterstitial()
{
...
// 广告请求成功加载时调用。
this.interstitial.OnAdLoaded += HandleOnAdLoaded;
// 广告请求加载失败时调用。
this.interstitial.OnAdFailedToLoad += HandleOnAdFailedToLoad;
// 广告展示时调用。
this.interstitial.OnAdOpening += HandleOnAdOpening;
// 广告关闭时调用。
this.interstitial.OnAdClosed += HandleOnAdClosed;
...
}
public void HandleOnAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdLoaded event received");
}
public void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
MonoBehaviour.print("HandleFailedToReceiveAd event received with message: "
+ args.Message);
}
public void HandleOnAdOpening(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdOpening event received");
}
public void HandleOnAdClosed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdClosed event received");
}
...
private void RequestInterstitial()
{
...
// 广告请求成功加载时调用。
this.interstitial.OnInterstitialLoaded += this.HandleInterstitialLoaded;
// 广告请求加载失败时调用。
this.interstitial.OnInterstitialFailedToLoad += this.HandleInterstitialFailedToLoad;
// 当用户点击后返回应用程序时调用。
this.interstitial.OnReturnedToApplication += this.HandleReturnedToApplication;
// 当用户点击广告后即将离开应用时调用。
this.interstitial.OnLeftApplication += this.HandleLeftApplication;
// 当用户点击广告时调用。
this.interstitial.OnAdClicked += this.HandleAdClicked;
// 广告展示时调用。
this.interstitial.OnInterstitialShown += this.HandleInterstitialShown;
// 广告关闭时调用。
this.interstitial.OnInterstitialDismissed += this.HandleInterstitialDismissed;
// 当跟踪到一次展示时调用。
this.interstitial.OnImpression += this.HandleImpression;
// 广告请求无法显示时调用。
this.interstitial.OnInterstitialFailedToShow += this.HandleInterstitialFailedToShow;
...
}
public void HandleInterstitialLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialLoaded event received");
}
public void HandleInterstitialFailedToLoad(object sender, AdFailureEventArgs args)
{
MonoBehaviour.print(
"HandleInterstitialFailedToLoad event received with message: " + args.Message);
}
public void HandleReturnedToApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleReturnedToApplication event received");
}
public void HandleLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleLeftApplication event received");
}
public void HandleAdClicked(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdClicked event received");
}
public void HandleInterstitialShown(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialShown event received");
}
public void HandleInterstitialDismissed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleInterstitialDismissed event received");
}
public void HandleImpression(object sender, ImpressionData impressionData)
{
var data = impressionData == null ? "null" : impressionData.rawData;
MonoBehaviour.print("HandleImpression event received with data: " + data);
}
public void HandleInterstitialFailedToShow(object sender, AdFailureEventArgs args)
{
MonoBehaviour.print(
"HandleInterstitialFailedToShow event received with message: " + args.Message);
}
激励广告
创建广告对象
Google AdMob (ex. AdMob)
Yandex Ads
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
...
private RewardedAd rewardedAd;
private void RequestRewarded()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
string adUnitId = "unexpected_platform";
#endif
this.rewardedAd = new RewardedAd(adUnitId);
}
using YandexMobileAds;
using YandexMobileAds.Base;
...
private RewardedAd rewardedAd;
private void RequestRewarded()
{
#if UNITY_ANDROID
string adUnitId = "demo-rewarded-yandex";
#elif UNITY_IPHONE
string adUnitId = "demo-rewarded-yandex";
#else
string adUnitId = "unexpected_platform";
#endif
this.rewardedAd = new RewardedAd(adUnitId);
}
加载广告
Google AdMob (ex. AdMob)
Yandex Ads
// 创建一个空的广告请求。
AdRequest request = new AdRequest.Builder().Build();
// 使用请求加载激励广告。
this.rewardedAd.LoadAd(request);
// 创建一个空的广告请求。
AdRequest request = new AdRequest.Builder().Build();
// 加载请求加载激励广告。
this.rewardedAd.LoadAd(request);
展示广告
Google AdMob (ex. AdMob)
Yandex Ads
private void ShowRewardedAd()
{
if (this.rewardedAd.IsLoaded()) {
this.rewardedAd.Show();
} else
{
Debug.Log("Rewarded ad is not ready yet");
}
}
private void ShowRewardedAd()
{
if (this.rewardedAd.IsLoaded())
{
rewardedAd.Show();
}
else
{
Debug.Log("Rewarded Ad is not ready yet");
}
}
清除广告
Google AdMob (ex. AdMob)
Yandex Ads
rewardedAd.Destroy();
rewardedAd.Destroy();
设置广告回调
Google AdMob (ex. AdMob)
Yandex Ads
...
private void RequestRewarded()
{
...
// 广告请求成功加载时调用。
this.rewardedAd.OnAdLoaded += HandleRewardedAdLoaded;
// 广告请求加载失败时调用。
this.rewardedAd.OnAdFailedToLoad += HandleRewardedAdFailedToLoad;
// 广告展示时调用。
this.rewardedAd.OnAdOpening += HandleRewardedAdOpening;
// 广告请求无法显示时调用。
this.rewardedAd.OnAdFailedToShow += HandleRewardedAdFailedToShow;
// 当用户因与广告互动而应获得奖励时调用。
this.rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
// 广告关闭时调用。
this.rewardedAd.OnAdClosed += HandleRewardedAdClosed;
...
}
public void HandleRewardedAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardedAdLoaded event received");
}
public void HandleRewardedAdFailedToLoad(object sender, AdFailedToLoadEventArgs 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);
}
...
private void RequestRewarded()
{
...
// 广告请求成功加载时调用。
this.rewardedAd.OnRewardedAdLoaded += this.HandleRewardedAdLoaded;
// 广告请求加载失败时调用。
this.rewardedAd.OnRewardedAdFailedToLoad += this.HandleRewardedAdFailedToLoad;
// 当用户点击后返回应用程序时调用。
this.rewardedAd.OnReturnedToApplication += this.HandleReturnedToApplication;
// 当用户点击广告后即将离开应用时调用。
this.rewardedAd.OnLeftApplication += this.HandleLeftApplication;
// 当用户点击广告时调用。
this.rewardedAd.OnAdClicked += this.HandleAdClicked;
// 广告展示时调用。
this.rewardedAd.OnRewardedAdShown += this.HandleRewardedAdShown;
// 广告关闭时调用。
this.rewardedAd.OnRewardedAdDismissed += this.HandleRewardedAdDismissed;
// 当跟踪到一次展示时调用。
this.rewardedAd.OnImpression += this.HandleImpression;
// 当用户因与广告互动而应获得奖励时调用。
this.rewardedAd.OnRewarded += this.HandleRewarded;
// 广告请求无法显示时调用。
this.rewardedAd.OnRewardedAdFailedToShow += this.HandleRewardedAdFailedToShow;
...
}
public void HandleRewardedAdLoaded(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardedAdLoaded event received");
}
public void HandleRewardedAdFailedToLoad(object sender, AdFailureEventArgs args)
{
MonoBehaviour.print(
"HandleRewardedAdFailedToLoad event received with message: " + args.Message);
}
public void HandleReturnedToApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleReturnedToApplication event received");
}
public void HandleLeftApplication(object sender, EventArgs args)
{
MonoBehaviour.print("HandleLeftApplication event received");
}
public void HandleAdClicked(object sender, EventArgs args)
{
MonoBehaviour.print("HandleAdClicked event received");
}
public void HandleRewardedAdShown(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardedAdShown event received");
}
public void HandleRewardedAdDismissed(object sender, EventArgs args)
{
MonoBehaviour.print("HandleRewardedAdDismissed event received");
}
public void HandleImpression(object sender, ImpressionData impressionData)
{
var data = impressionData == null ? "null" : impressionData.rawData;
MonoBehaviour.print("HandleImpression event received with data: " + data);
}
public void HandleRewarded(object sender, Reward args)
{
MonoBehaviour.print("HandleRewarded event received: amout = " + args.amount + ", type = " + args.type);
}
public void HandleRewardedAdFailedToShow(object sender, AdFailureEventArgs args)
{
MonoBehaviour.print(
"HandleRewardedAdFailedToShow event received with message: " + args.Message);
}
横幅广告
创建广告对象
Google AdMob (ex. AdMob)
Yandex Ads
using GoogleMobileAds.Api;
using GoogleMobileAds.Common;
...
private BannerView bannerView;
private void RequestBanner()
{
#if UNITY_ANDROID
string adUnitId = "ca-app-pub-3940256099942544/6300978111";
#elif UNITY_IPHONE
string adUnitId = "ca-app-pub-3940256099942544/2934735716";
#else
string adUnitId = "unexpected_platform";
#endif
// 在屏幕顶部创建一个 320x50 的横幅。
this.bannerView = new BannerView(adUnitId, AdSize.Banner, AdPosition.Top);
}
using YandexMobileAds;
using YandexMobileAds.Base;
...
private Banner bannerView;
private void RequestBanner()
{
#if UNITY_ANDROID
string adUnitId = "demo-banner-yandex";
#elif UNITY_IPHONE
string adUnitId = "demo-banner-yandex";
#else
string adUnitId = "unexpected_platform";
#endif
// 在屏幕顶部创建一个 320x50 的横幅。
this.bannerView = new Banner(adUnitId, AdSize.BANNER_320x50, AdPosition.TopCenter);
}
设置横幅尺寸
除了常量之外,该插件还允许您设置任意横幅尺寸:
Google AdMob (ex. AdMob)
Yandex Ads
AdSize adSize = new AdSize(250, 250);
BannerView bannerView = new BannerView(adUnitId, adSize, AdPosition.Bottom);
AdSize adSize = AdSize.FlexibleSize(250, 250)
BannerView bannerView = new BannerView(adUnitId, adSize, AdPosition.BottomCenter);
设置在屏幕上的位置
Google AdMob (ex. AdMob)
Yandex Ads
// 屏幕底部中心
BannerView bannerView = new BannerView(adUnitId, adSize, AdPosition.Bottom);
// 自定义坐标
BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, 0, 50);
// 屏幕底部中心
BannerView bannerView = new BannerView(adUnitId, adSize, AdPosition.BottomCenter);
// Yandex Ads 不支持自定义坐标
加载广告
Google AdMob (ex. AdMob)
Yandex Ads
// 创建一个空的广告请求。
AdRequest request = new AdRequest.Builder().Build();
// 使用请求加载横幅。
this.bannerView.LoadAd(request);
// 创建一个空的广告请求。
AdRequest request = new AdRequest.Builder().Build();
// 使用请求加载横幅。
this.bannerView.LoadAd(request);
清除广告
Google AdMob (ex. AdMob)
Yandex Ads
bannerView.Destroy();
bannerView.Destroy();