מודעות מתגמלות הן מודעות שהמשתמשים יכולים לבחור ליצור איתן אינטראקציה בתמורה לתגמולים בתוך האפליקציה. במדריך הזה מוסבר איך לשלב מודעות מתגמלות מ-AdMob באפליקציית Unity.
מומלץ לקרוא כמה סיפורי הצלחה של לקוחות: מקרה לדוגמה 1, מקרה לדוגמה 2.במדריך הזה מוסבר איך לשלב מודעות מתגמלות באפליקציית Unity.
דרישות מוקדמות
- כדאי לעיין במדריך לתחילת העבודה.
תמיד כדאי לבצע בדיקות באמצעות מודעות בדיקה
דוגמת הקוד הבאה מכילה מזהה של יחידת מודעות שבו אפשר להשתמש כדי לבקש מודעות בדיקה. הוא הוגדר במיוחד להחזרת מודעות בדיקה ולא מודעות פעילות בכל בקשה, ולכן השימוש בו בטוח.
עם זאת, אחרי שרושמים אפליקציה בממשק האינטרנט של AdMob ויוצרים מזהים של יחידות מודעות לשימוש באפליקציה, צריך להגדיר את המכשיר כמכשיר בדיקה באופן מפורש במהלך הפיתוח.
Android
ca-app-pub-3940256099942544/5224354917
iOS
ca-app-pub-3940256099942544/1712485313
הפעלה של Mobile Ads SDK
לפני טעינת המודעות, האפליקציה צריכה להפעיל את Mobile Ads SDK באמצעות קריאה ל-MobileAds.Initialize()
. צריך לבצע את הפעולה הזו רק פעם אחת, ועדיף בזמן הפעלת האפליקציה.
using GoogleMobileAds;
using GoogleMobileAds.Api;
public class GoogleMobileAdsDemoScript : MonoBehaviour
{
public void Start()
{
// Initialize the Google Mobile Ads SDK.
MobileAds.Initialize((InitializationStatus initStatus) =>
{
// This callback is called once the MobileAds SDK is initialized.
});
}
}
אם אתם משתמשים בתהליך בחירת רשת, צריך להמתין עד שהקריאה החוזרת (callback) תתרחש לפני טעינת המודעות, כדי לוודא שכל המתאמים של תהליך בחירת הרשת אותחלו.
הטמעה
השלבים העיקריים לשילוב מודעות מתגמלות הם:
- טעינת המודעה המתגמלת
- [אופציונלי] אימות של קריאות חוזרות (callback) של אימות בצד השרת (SSV)
- הצגת המודעה המתגמלת עם קריאה חוזרת (callback) של התגמול
- האזנה לאירועים של מודעות מתגמלות
- ניקוי המודעה המתגמלת
- טעינה מראש של המודעה המתגמלת הבאה
טעינת המודעה המתגמלת
טעינה של מודעה מתגמלת מתבצעת באמצעות השיטה הסטטית Load()
במחלקה RewardedAd
. אובייקט RewardedAd
שנטען מסופק כפרמטר ב-completion handler. בדוגמה הבאה מוצג איך לטעון RewardedAd
.
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
private string _adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
private string _adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
private string _adUnitId = "unused";
#endif
private RewardedAd _rewardedAd;
/// <summary>
/// Loads the rewarded ad.
/// </summary>
public void LoadRewardedAd()
{
// Clean up the old ad before loading a new one.
if (_rewardedAd != null)
{
_rewardedAd.Destroy();
_rewardedAd = null;
}
Debug.Log("Loading the rewarded ad.");
// create our request used to load the ad.
var adRequest = new AdRequest();
// send the request to load the ad.
RewardedAd.Load(_adUnitId, adRequest,
(RewardedAd ad, LoadAdError error) =>
{
// if error is not null, the load request failed.
if (error != null || ad == null)
{
Debug.LogError("Rewarded ad failed to load an ad " +
"with error : " + error);
return;
}
Debug.Log("Rewarded ad loaded with response : "
+ ad.GetResponseInfo());
_rewardedAd = ad;
});
}
[אופציונלי] אימות של קריאות חוזרות (callback) של אימות בצד השרת (SSV)
באפליקציות שנדרשים בהן נתונים נוספים בקריאות חוזרות (callback) של אימות בצד השרת, צריך להשתמש בתכונה 'נתונים מותאמים אישית' של מודעות מתגמלות.
כל ערך מחרוזת שמוגדר באובייקט של מודעה מתגמלת מועבר לפרמטר השאילתה custom_data
של הקריאה החוזרת של SSV. אם לא מוגדר ערך של נתונים מותאמים אישית, הערך של פרמטר השאילתה custom_data
לא יופיע בקריאה החוזרת של SSV.
דוגמת הקוד הבאה מראה איך להגדיר את האפשרויות של SSV אחרי שהמודעה המתגמלת נטענת.
// send the request to load the ad.
RewardedAd.Load(_adUnitId, adRequest, (RewardedAd ad, LoadAdError error) =>
{
// If the operation failed, an error is returned.
if (error != null || ad == null)
{
Debug.LogError("Rewarded ad failed to load an ad with error : " + error);
return;
}
// If the operation completed successfully, no error is returned.
Debug.Log("Rewarded 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);
});
אם רוצים להגדיר את מחרוזת התגמול בהתאמה אישית, צריך לעשות זאת לפני הצגת המודעה.
הצגת המודעה המתגמלת עם קריאה חוזרת (callback) של התגמול
כשמציגים את המודעה, צריך לספק קריאה חוזרת לטיפול בתגמול עבור המשתמש. מודעות יכולות להופיע רק פעם אחת בכל טעינה. משתמשים ב-method CanShowAd()
כדי לוודא שהמודעה מוכנה להצגה.
הקוד הבא מציג את השיטה הכי טובה להצגת מודעה מתגמלת.
public void ShowRewardedAd()
{
const string rewardMsg =
"Rewarded ad rewarded the user. Type: {0}, amount: {1}.";
if (rewardedAd != null && rewardedAd.CanShowAd())
{
rewardedAd.Show((Reward reward) =>
{
// TODO: Reward the user.
Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
});
}
}
האזנה לאירועים של מודעות מתגמלות
כדי להתאים אישית עוד יותר את התנהגות המודעה, אפשר להשתמש במספר אירועים במחזור החיים של המודעה: פתיחה, סגירה וכן הלאה. כדי להאזין לאירועים האלה, צריך לרשום נציג כמו שמוצג בהמשך.
private void RegisterEventHandlers(RewardedAd ad)
{
// Raised when the ad is estimated to have earned money.
ad.OnAdPaid += (AdValue adValue) =>
{
Debug.Log(String.Format("Rewarded ad paid {0} {1}.",
adValue.Value,
adValue.CurrencyCode));
};
// Raised when an impression is recorded for an ad.
ad.OnAdImpressionRecorded += () =>
{
Debug.Log("Rewarded ad recorded an impression.");
};
// Raised when a click is recorded for an ad.
ad.OnAdClicked += () =>
{
Debug.Log("Rewarded ad was clicked.");
};
// Raised when an ad opened full screen content.
ad.OnAdFullScreenContentOpened += () =>
{
Debug.Log("Rewarded ad full screen content opened.");
};
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Rewarded ad full screen content closed.");
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Rewarded ad failed to open full screen content " +
"with error : " + error);
};
}
ניקוי המודעה המתגמלת
כשמסיימים להשתמש ב-RewardedAd
, חשוב לקרוא לשיטת Destroy()
לפני שמבטלים את ההפניה אליו:
_rewardedAd.Destroy();
ההודעה הזו מודיעה לתוסף שהאובייקט כבר לא בשימוש, ושאפשר לפנות את הזיכרון שהוא תופס. אם לא קוראים לשיטה הזו, מתרחשים דליפות זיכרון.
טעינה מראש של המודעה המתגמלת הבאה
RewardedAd
הוא אובייקט לשימוש חד-פעמי. המשמעות היא שאחרי שמודעה מתגמלת מוצגת, אי אפשר להשתמש שוב באובייקט. כדי לבקש עוד מודעה מתגמלת, צריך ליצור אובייקט RewardedAd
חדש.
כדי להכין מודעה מתגמלת להזדמנות החשיפה הבאה, צריך לטעון מראש את המודעה המתגמלת אחרי שמופעל אירוע המודעה OnAdFullScreenContentClosed
או OnAdFullScreenContentFailed
.
private void RegisterReloadHandler(RewardedAd ad)
{
// Raised when the ad closed full screen content.
ad.OnAdFullScreenContentClosed += () =>
{
Debug.Log("Rewarded Ad full screen content closed.");
// Reload the ad so that we can show another as soon as possible.
LoadRewardedAd();
};
// Raised when the ad failed to open full screen content.
ad.OnAdFullScreenContentFailed += (AdError error) =>
{
Debug.LogError("Rewarded ad failed to open full screen content " +
"with error : " + error);
// Reload the ad so that we can show another as soon as possible.
LoadRewardedAd();
};
}
מקורות מידע נוספים
- דוגמה ל-HelloWorld: הטמעה מינימלית של כל הפורמטים של המודעות.