מודעות מתגמלות

מודעות מתגמלות הן מודעות שמשתמשים יכולים ליצור איתן אינטראקציה בתמורה ומקבלים פרסים באפליקציה. במדריך הזה מוסבר איך לשלב מודעות מתגמלות פתיחת AdMob באפליקציה ל-Unity.

במדריך הזה מוסבר איך לשלב מודעות מתגמלות באפליקציה של Unity.

דרישות מוקדמות

ביצוע בדיקות באמצעות מודעות בדיקה תמיד

הקוד לדוגמה הבא מכיל מזהה של יחידת מודעות שאפשר להשתמש בו כדי לבקש מודעות לבדיקה. הוא הוגדר במיוחד להחזרת מודעות בדיקה במקום מודעות בסביבת הייצור לכל בקשה, כך שזה בטוח לשימוש.

עם זאת, אחרי שרשמתם אפליקציה ממשק האינטרנט של Ad Manager ונוצרה יחידת מודעות משלך מזהים לשימוש באפליקציה, צריך להגדיר את המכשיר כבדיקה באופן מפורש המכשיר במהלך ופיתוח.

/6499/example/rewarded

הפעלה של 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.
        });
    }
}

אם בחרת להשתמש בתהליך בחירת הרשת (Mediation), עליך להמתין עד שהקריאה החוזרת תתבצע לפני טעינת המודעות בתור פעולה זו תבטיח שכל המתאמים לתהליך בחירת הרשת (Mediation) יופעלו.

הטמעה

השלבים העיקריים לשילוב מודעות מתגמלות הם:

  1. טעינת המודעה המתגמלת
  2. [אופציונלי] אימות קריאות חוזרות (callback) של אימות בצד השרת (SSV)
  3. הצגת המודעה המתגמלת עם קריאה חוזרת (callback) של הפרס
  4. איך מקשיבים לאירועים של מודעות מתגמלות
  5. מחיקת המודעה המתגמלת
  6. טעינה מראש של המודעה המתגמלת הבאה

טעינת המודעה המתגמלת

טעינה של מודעה מתגמלת מתבצעת באמצעות שיטת Load() הסטטית כיתה אחת (RewardedAd). האובייקט RewardedAd שנטען מסופק בתור ב-handler של ההשלמה האוטומטית. הדוגמה הבאה מראה איך לטעון RewardedAd.


  // This ad unit is configured to always serve test ads.
  private string _adUnitId = "/6499/example/rewarded";

  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 AdManagerAdRequest();

      // 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) של הפרס

כשמציגים את המודעה, צריך לבצע קריאה חוזרת כדי לטפל בתגמול למשתמש. ניתן להציג מודעות פעם אחת בלבד בכל טעינה. אפשר להשתמש בשיטה 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();
    };
}

משאבים נוספים