Миграция с Google AdMob (ex. AdMob) на медиацию Яндекса на Unity
Чтобы заменить рекламу Google AdMob (ex. AdMob) на медиацию Yandex Ads на Unity, внесите в свой код следующие изменения.
Подключение SDK
Библиотеки распространяются через Unity Play Services Resolver library. Добавьте в проект ресурс плагина:
Google AdMob (ex. AdMob)
Yandex Ads
Выберите Assets → Import Package → Custom Package и найдите загруженный файл GoogleMobileAdsPlugin.unitypackage
.
Выберите Assets → Import Package → Custom Package и найдите загруженный файл yandex-mobileads-lite-2.7.0.unitypackage
. Импортируйте из каталога mobileads-mediation
пакет mobileads-admob-mediation-2.7.0.unitypackage
.
Перед загрузкой рекламы инициализируйте библиотеку:
Google AdMob (ex. AdMob)
Yandex Ads
...
using GoogleMobileAds.Api;
...
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public void Start()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize(initStatus => { });
}
}
// Не требуется
Форматы объявлений
Interstitial
Создание объекта рекламы
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
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
this.interstitial.LoadAd(request);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
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()
{
...
// Called when an ad request has successfully loaded.
this.interstitial.OnAdLoaded += HandleOnAdLoaded;
// Called when an ad request failed to load.
this.interstitial.OnAdFailedToLoad += HandleOnAdFailedToLoad;
// Called when an ad is shown.
this.interstitial.OnAdOpening += HandleOnAdOpening;
// Called when the ad is closed.
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()
{
...
// Called when an ad request has successfully loaded.
this.interstitial.OnInterstitialLoaded += this.HandleInterstitialLoaded;
// Called when an ad request failed to load.
this.interstitial.OnInterstitialFailedToLoad += this.HandleInterstitialFailedToLoad;
// Called when user returned to application after click.
this.interstitial.OnReturnedToApplication += this.HandleReturnedToApplication;
// Called when user is about to leave application after tapping on an ad.
this.interstitial.OnLeftApplication += this.HandleLeftApplication;
// Called when user clicked on the ad.
this.interstitial.OnAdClicked += this.HandleAdClicked;
// Called when an ad is shown.
this.interstitial.OnInterstitialShown += this.HandleInterstitialShown;
// Called when the ad is closed.
this.interstitial.OnInterstitialDismissed += this.HandleInterstitialDismissed;
// Called when an impression was tracked
this.interstitial.OnImpression += this.HandleImpression;
// Called when an ad request failed to show.
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);
}
Rewarded
Создание объекта рекламы
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
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded ad with the request.
this.rewardedAd.LoadAd(request);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the rewarded with the request.
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()
{
...
// 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;
...
}
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()
{
...
// Called when an ad request has successfully loaded.
this.rewardedAd.OnRewardedAdLoaded += this.HandleRewardedAdLoaded;
// Called when an ad request failed to load.
this.rewardedAd.OnRewardedAdFailedToLoad += this.HandleRewardedAdFailedToLoad;
// Called when user returned to application after click.
this.rewardedAd.OnReturnedToApplication += this.HandleReturnedToApplication;
// Called when user is about to leave application after tapping on an ad.
this.rewardedAd.OnLeftApplication += this.HandleLeftApplication;
// Called when user clicked on the ad.
this.rewardedAd.OnAdClicked += this.HandleAdClicked;
// Called when an ad is shown.
this.rewardedAd.OnRewardedAdShown += this.HandleRewardedAdShown;
// Called when the ad is closed.
this.rewardedAd.OnRewardedAdDismissed += this.HandleRewardedAdDismissed;
// Called when an impression was tracked
this.rewardedAd.OnImpression += this.HandleImpression;
// Called when the user should be rewarded for interacting with the ad.
this.rewardedAd.OnRewarded += this.HandleRewarded;
// Called when an ad request failed to show.
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);
}
Banner
Создание объекта рекламы
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
// Create a 320x50 banner at the top of the screen.
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
// Create a 320x50 banner at the top of the screen.
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
// Bottom center on the screen
BannerView bannerView = new BannerView(adUnitId, adSize, AdPosition.Bottom);
// custom coordinate
BannerView bannerView = new BannerView(adUnitId, AdSize.Banner, 0, 50);
// Bottom center on the screen
BannerView bannerView = new BannerView(adUnitId, adSize, AdPosition.BottomCenter);
// custom coordinate not possible in Yandex Ads
Загрузка рекламы
Google AdMob (ex. AdMob)
Yandex Ads
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the banner with the request.
this.bannerView.LoadAd(request);
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the banner with the request.
this.bannerView.LoadAd(request);
Очистка рекламы
Google AdMob (ex. AdMob)
Yandex Ads
bannerView.Destroy();
bannerView.Destroy();