بینابینی پاداشدار نوعی قالب آگهی تشویقی است که به شما امکان میدهد برای آگهیهایی که بهطور خودکار درطول انتقالهای طبیعی برنامه ظاهر میشوند پاداش ارائه دهید. برخلاف آگهیهای پاداشدار، کاربران برای مشاهده آگهی بینابینی پاداشدار ملزم به موافقت نیستند.
پیشنیازها
پیشاز ادامه دادن، Google Mobile Ads Unity Plugin را راهاندازی کنید.
همیشه با آگهیهای آزمایشی آزمایش کنید
کد نمونه زیر حاوی شناسه واحد آگهی است که میتوانید از آن برای درخواست آگهیهای آزمایشی استفاده کنید. این شناسه بهطور ویژه پیکربندی شده است تا برای هر درخواست، آگهیهای آزمایشی را بهجای آگهیهای تولیدی برگرداند و استفاده از آن را ایمن کند.
بااینحال، پساز ثبت برنامه در میانای وب AdMob و ایجاد شناسههای واحد آگهی خودتان برای استفاده در برنامه، صریحاً دستگاهتان را بهعنوان دستگاه آزمایشی پیکربندی کنید درطول توسعه.
Android
ca-app-pub-3940256099942544/1033173712
iOS
ca-app-pub-3940256099942544/4411468910
مقداردهی اولیه Google Mobile Ads Unity Plugin
قبلاز بار کردن آگهیها، با فراخوانی MobileAds.Initialize()، برنامه خود را مقداردهی اولیه Google Mobile Ads Unity Plugin کنید. این کار فقط یکبار باید انجام شود، ترجیحاً هنگام راهاندازی برنامه.
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public void Start()
{
// Initialize Google Mobile Ads Unity Plugin.
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
});
}
}
اگر از میانجیگری استفاده میکنید، قبلاز بار کردن آگهیها صبر کنید تا فراخوانی برگشتی انجام شود زیرا این کار تضمین میکند که همه آداپتورهای میانجیگری مقداردهی اولیه شدهاند.
پیادهسازی
مراحل اصلی برای ادغام آگهیهای بینابینی پاداشدار عبارتند از:
- بار کردن آگهی بینابینی پاداشدار
- [اختیاری] اعتبارسنجی تماسهای برگشتی درستیسنجی سمت سرور (SSV)
- نمایش آگهی بینابینی پاداشدار با فراخوان پاداش
- گوش دادن به رویدادهای آگهی بینابینی پاداشدار
- پاکسازی آگهی بینابینی پاداشدار
- پیشبار کردن آگهی بینابینی پاداشدار بعدی
بار کردن آگهی بینابینی پاداشدار
بار کردن آگهی بینابینی پاداشدار بااستفاده از روش ثابت Load()
در کلاس RewardedInterstitialAd انجام میشود. روش بار کردن به شناسه واحد آگهی، شیء AdRequest، و گرداننده تکمیل نیاز دارد که وقتی بار کردن آگهی موفقیتآمیز یا ناموفق باشد فراخوانده میشود. شیء RewardedInterstitialAd بارشده بهعنوان پارامتری در گرداننده تکمیل ارائه میشود. مثال زیر نحوه بار کردن RewardedInterstitialAd را نشان میدهد.
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
private string _adUnitId = "ca-app-pub-3940256099942544/5354046379";
#elif UNITY_IPHONE
private string _adUnitId = "ca-app-pub-3940256099942544/6978759866";
#else
private string _adUnitId = "unused";
#endif
private RewardedInterstitialAd _rewardedInterstitialAd;
/// <summary>
/// Loads the rewarded interstitial ad.
/// </summary>
public void LoadRewardedInterstitialAd()
{
// Clean up the old ad before loading a new one.
if (_rewardedInterstitialAd != null)
{
_rewardedInterstitialAd.Destroy();
_rewardedInterstitialAd = null;
}
Debug.Log("Loading the rewarded interstitial ad.");
// create our request used to load the ad.
var adRequest = new AdRequest();
adRequest.Keywords.Add("unity-admob-sample");
// send the request to load the ad.
RewardedInterstitialAd.Load(_adUnitId, adRequest,
(RewardedInterstitialAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("rewarded interstitial ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Rewarded interstitial ad loaded with response : "
+ ad.GetResponseInfo());
_rewardedInterstitialAd = ad;
});
}
[اختیاری] اعتبارسنجی تماسهای برگشتی درستیسنجی سمت سرور (SSV)
برنامههایی که به دادههای اضافی در بازخوانیهای درستیسنجی سمت سرور نیاز دارند باید از ویژگی دادههای سفارشی آگهیهای بینابینی پاداشدار استفاده کنند.
هر مقدار رشتهای که در شیء آگهی پاداشدار تنظیم شده باشد به پارامتر پُرسمان custom_data
در فراخوانی SSV ارسال میشود. اگر مقدار داده سفارشی تنظیم نشده باشد، مقدار پارامتر پُرسمان custom_data در فراخوانی SSV گنجانده نخواهد شد.
نمونه کد زیر نشان میدهد که چگونه گزینههای SSV را پساز بارگیری آگهی بینابینی پاداشدار تنظیم کنید.
// send the request to load the ad.
RewardedInterstitialAd.Load(_adUnitId,
adRequest,
(RewardedInterstitialAd ad, LoadAdError error) =>
{
// If the operation failed, an error is returned.
if (error != null || ad == null)
{
Debug.LogError("Rewarded interstitial ad failed to load an ad " +
" with error : " + error);
return;
}
// If the operation completed successfully, no error is returned.
Debug.Log("Rewarded interstitial ad loaded with response : " +
ad.GetResponseInfo());
// Create and pass the SSV options to the rewarded ad.
var options = new ServerSideVerificationOptions
.Builder()
.SetCustomData("SAMPLE_CUSTOM_DATA_STRING")
.Build()
ad.SetServerSideVerificationOptions(options);
});
اگر میخواهید رشته پاداش سفارشی را تنظیم کنید، باید این کار را قبلاز نمایش آگهی انجام دهید.
نمایش آگهی بینابینی پاداشدار با فراخوان پاداش
هنگام ارائه آگهی، باید یک فراخوان برگشتی برای مدیریت پاداش کاربر ارائه دهید. آگهیها فقط یکبار در هر بارگیری نشان داده میشوند. از روش CanShowAd() برای
درستیسنجی آماده بودن آگهی برای نمایش استفاده کنید.
کد زیر بهترین روش را برای نمایش آگهی تمامصفحه پاداشدار ارائه میدهد.
public void ShowRewardedInterstitialAd()
{
const string rewardMsg =
"Rewarded interstitial ad rewarded the user. Type: {0}, amount: {1}.";
if (rewardedInterstitialAd != null && rewardedInterstitialAd.CanShowAd())
{
rewardedInterstitialAd.Show((Reward reward) =>
{
// TODO: Reward the user.
Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
});
}
}
گوش دادن به رویدادهای آگهی بینابینی پاداشدار
برای سفارشیسازی بیشتر رفتار آگهیتان، میتوانید به تعدادی از رویدادها در چرخه حیات آگهی متصل شوید. با ثبت نماینده، همانطور که در زیر نشان داده شده است، به این رویدادها گوش دهید.
private void RegisterEventHandlers(RewardedInterstitialAd ad)
{
// Raised when the ad is estimated to have earned money.
ad.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(String.Format("Rewarded interstitial ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
// Raised when an impression is recorded for an ad.
ad.OnAdImpressionRecorded += () =>
{
Debug.Log("Rewarded interstitial ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
ad.OnAdClicked += () =>
{
Debug.Log("Rewarded interstitial ad was clicked.");
};
// Raised when an ad opened full screen content.
ad.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Rewarded interstitial ad full screen content opened.");
};
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Rewarded interstitial ad full screen content closed.");
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Rewarded interstitial ad failed to open " +
"full screen content with error : " + error);
};
}
پاکسازی آگهی بینابینی پاداشدار
وقتی کارتان با RewardedInterstitialAd تمام شد، حتماً پیشاز رها کردن ارجاع به آن، روش Destroy() را فراخوانی کنید:
_rewardedInterstitialAd.Destroy();
این کار به افزونه اطلاع میدهد که دیگر از شیء استفاده نمیشود و حافظهای که اشغال کرده است میتواند بازپس گرفته شود. عدم فراخوانی این روش منجر به نشت حافظه میشود.
پیشبار کردن آگهی بینابینی پاداشدار بعدی
RewardedInterstitialAd شیء یکبار مصرف است. این یعنی وقتی آگهی تمامصفحه پاداشدار نشان داده شد، دیگر نمیتوان از آن شیء استفاده کرد. برای درخواست آگهی تمامصفحه پاداشدار دیگر، باید شیء RewardedInterstitialAd جدیدی بار کنید.
برای آماده کردن آگهی بینابینی پاداشدار برای فرصت ظهور بعدی،
آگهی بینابینی پاداشدار را بهمحض مطرح شدن رویداد آگهی OnAdFullScreenContentClosed یا
OnAdFullScreenContentFailed پیشبار کنید.
private void RegisterReloadHandler(RewardedInterstitialAd ad)
{
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += ()
{
Debug.Log("Rewarded interstitial ad full screen content closed.");
// Reload the ad so that we can show another as soon as possible.
LoadRewardedInterstitialAd();
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Rewarded interstitial ad failed to open " +
"full screen content with error : " + error);
// Reload the ad so that we can show another as soon as possible.
LoadRewardedInterstitialAd();
};
}
منابع بیشتر
- نمونه HelloWorld: پیادهسازی حداقلی همه قالبهای آگهی.