โฆษณาที่มีการให้รางวัลคือโฆษณาที่ผู้ใช้เลือกโต้ตอบด้วยเพื่อแลกกับรางวัลในแอปได้ คู่มือนี้จะแสดงวิธีผสานรวมโฆษณาที่มีการให้รางวัล จาก AdMob เข้ากับแอป Unity
อ่านเรื่องราวความสำเร็จของลูกค้า: กรณีศึกษา 1 กรณีศึกษา 2คู่มือนี้อธิบายวิธีผสานรวมโฆษณาที่มีการให้รางวัลเข้ากับแอป Unity
ข้อกำหนดเบื้องต้น
- ทำตามคู่มือเริ่มต้นใช้งาน
ทดสอบด้วยโฆษณาทดสอบเสมอ
โค้ดตัวอย่างด้านล่างประกอบด้วยรหัสหน่วยโฆษณาที่คุณใช้ขอโฆษณาทดสอบได้ ได้รับการกำหนดค่ามาเป็นพิเศษให้แสดงโฆษณาทดสอบแทนโฆษณาจริงสำหรับทุกคำขอ จึงทำให้ใช้งานได้อย่างปลอดภัย
แต่หลังจากที่คุณได้ลงทะเบียนแอปใน AdMob เว็บอินเทอร์เฟซและ สร้างรหัสหน่วยโฆษณาของคุณเองไว้ใช้ในแอปแล้ว ให้กำหนดค่าอุปกรณ์ของคุณเป็นอุปกรณ์ทดสอบอย่างชัดเจนระหว่าง การพัฒนา
Android
ca-app-pub-3940256099942544/5224354917
iOS
ca-app-pub-3940256099942544/1712485313
เริ่มต้น SDK โฆษณาในอุปกรณ์เคลื่อนที่
ก่อนที่จะโหลดโฆษณา ให้แอปเริ่มต้น 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.
});
}
}
หากคุณใช้สื่อกลาง ให้รอจนกว่าการเรียกกลับจะเกิดขึ้นก่อนที่จะโหลดโฆษณา เนื่องจากวิธีนี้จะช่วยให้อะแดปเตอร์สื่อกลางทั้งหมดเริ่มทำงานแล้ว
การใช้งาน
ขั้นตอนหลักในการผสานรวมโฆษณาที่มีการให้รางวัลมีดังนี้
- โหลดโฆษณาที่มีการให้รางวัล
- [ไม่บังคับ] ตรวจสอบโค้ดเรียกกลับของการยืนยันฝั่งเซิร์ฟเวอร์ (SSV)
- แสดงโฆษณาที่มีการให้รางวัลพร้อมการโทรกลับรับรางวัล
- ฟังเหตุการณ์โฆษณาที่มีการให้รางวัล
- ล้างโฆษณาที่มีการให้รางวัล
- โหลดโฆษณาที่มีการให้รางวัลรายการต่อไปไว้ล่วงหน้า
โหลดโฆษณาที่มีการให้รางวัล
การโหลดโฆษณาที่มีการให้รางวัลสามารถทำได้โดยใช้เมธอด Load()
แบบคงที่ในชั้นเรียน RewardedAd
ออบเจ็กต์ RewardedAd
ที่โหลดจะระบุเป็นพารามิเตอร์ในตัวจัดการการเสร็จสมบูรณ์ ตัวอย่างด้านล่างแสดงวิธีโหลด 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;
});
}
[ไม่บังคับ] ตรวจสอบโค้ดเรียกกลับของการยืนยันฝั่งเซิร์ฟเวอร์ (SSV)
แอปที่ต้องใช้ข้อมูลเพิ่มเติมในโค้ดเรียกกลับการยืนยันฝั่งเซิร์ฟเวอร์ควรใช้ฟีเจอร์ข้อมูลที่กำหนดเองของโฆษณาที่มีการให้รางวัล
ระบบจะส่งค่าสตริงที่ตั้งค่าไว้ในออบเจ็กต์โฆษณาที่มีการให้รางวัลไปยังพารามิเตอร์การค้นหา 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);
});
หากต้องการตั้งค่าสตริงรางวัลที่กำหนดเอง คุณต้องดำเนินการก่อนที่จะแสดงโฆษณา
แสดงโฆษณาที่มีการให้รางวัลพร้อมการโทรกลับรับรางวัล
เมื่อแสดงโฆษณา คุณต้องให้โค้ดเรียกกลับเพื่อจัดการรางวัลให้กับผู้ใช้ โฆษณาแสดงได้ครั้งเดียวต่อการโหลด ใช้เมธอด 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: ใช้รูปแบบโฆษณาทั้งหมดเพียงเล็กน้อย