โฆษณาที่มีการให้รางวัลคือโฆษณาที่ผู้ใช้มีตัวเลือกในการโต้ตอบเพื่อแลกกับรางวัลในแอป คู่มือนี้จะแสดงวิธีรวมโฆษณาที่มีการให้รางวัลจาก AdMob เข้ากับแอป Unity
อ่านเรื่องราวความสําเร็จของลูกค้าดังนี้ กรณีศึกษา 1, กรณีศึกษา 2
คู่มือนี้จะอธิบายวิธีผสานรวมโฆษณาที่มีการให้รางวัลลงในแอป Unity
สิ่งที่ต้องดำเนินการก่อน
- ทําตามคู่มือเริ่มต้นใช้งานให้เสร็จสมบูรณ์
ทดสอบด้วยโฆษณาทดสอบเสมอ
โค้ดตัวอย่างด้านล่างมีรหัสหน่วยโฆษณาที่คุณใช้เพื่อขอโฆษณาทดสอบได้ และได้รับการกําหนดค่าเป็นพิเศษให้แสดงโฆษณาทดสอบแทนโฆษณาจริงสําหรับคําขอทั้งหมด ทําให้ใช้งานได้อย่างปลอดภัย
อย่างไรก็ตาม เมื่อคุณลงทะเบียนแอปใน UI ของ 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.Builder().Build();
// 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);
});
หากคุณต้องการตั้งค่าสตริงรางวัลที่กําหนดเอง คุณต้องทําก่อนแสดงโฆษณา
แสดงโฆษณาที่มีการให้รางวัลด้วยการติดต่อกลับรางวัล
เมื่อนําเสนอโฆษณา คุณต้องระบุการเรียกกลับเพื่อจัดการรางวัลสําหรับผู้ใช้ แสดงโฆษณาได้เพียง 1 ครั้งต่อการโหลด 1 ครั้ง ใช้เมธอด 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: ติดตั้งใช้งานรูปแบบโฆษณาทั้งหมดเพียงเล็กน้อย