โฆษณาที่มีการให้รางวัลคือโฆษณาที่ผู้ใช้มีตัวเลือกในการโต้ตอบเพื่อแลกเปลี่ยน เพื่อรับรางวัลในแอป คู่มือนี้แสดงวิธีผสานรวมโฆษณาที่มีการให้รางวัลจาก AdMob ลงในแอป Unity
คู่มือนี้จะอธิบายวิธีผสานรวมโฆษณาที่มีการให้รางวัลเข้ากับแอป Unity
ข้อกำหนดเบื้องต้น
- ทำตามคู่มือเริ่มต้นใช้งานจนจบ
ทดสอบด้วยโฆษณาทดสอบเสมอ
โค้ดตัวอย่างต่อไปนี้มีรหัสหน่วยโฆษณาที่คุณใช้เพื่อขอได้ โฆษณาทดสอบ เราได้กําหนดค่ามาเป็นพิเศษให้แสดงโฆษณาทดสอบแทนโฆษณาเวอร์ชันที่ใช้งานจริงสําหรับคําขอทุกรายการ จึงใช้งานได้อย่างปลอดภัย
อย่างไรก็ตาม หลังจากที่คุณลงทะเบียนแอปใน เว็บอินเทอร์เฟซของ Ad Manager และสร้างหน่วยโฆษณาของคุณเอง รหัสสำหรับใช้ในแอป ให้กำหนดค่าอุปกรณ์เป็นการทดสอบให้ชัดเจน อุปกรณ์ในระหว่าง ที่กำลังพัฒนา
/21775744923/example/rewarded
เริ่มต้น 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 ของการยืนยันฝั่งเซิร์ฟเวอร์ (SSV)
- แสดงโฆษณาที่มีการให้รางวัลที่มี Callback รางวัล
- ฟังเหตุการณ์โฆษณาที่มีการให้รางวัล
- ล้างโฆษณาที่มีการให้รางวัล
- โหลดโฆษณาที่มีการให้รางวัลรายการถัดไปล่วงหน้า
โหลดโฆษณาที่มีการให้รางวัล
การโหลดโฆษณาที่มีการให้รางวัลดำเนินการได้โดยใช้เมธอด Load()
แบบคงที่ใน
ชั้นเรียนRewardedAd
ออบเจ็กต์ RewardedAd
ที่โหลดระบุเป็น
ในเครื่องจัดการการทำงานสำเร็จ ตัวอย่างด้านล่างแสดงวิธีโหลด
RewardedAd
// This ad unit is configured to always serve test ads.
private string _adUnitId = "/21775744923/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
พารามิเตอร์การค้นหาของ Callback SSV หากไม่ได้ตั้งค่าข้อมูลที่กำหนดเองไว้
ค่าพารามิเตอร์การค้นหา custom_data
จะไม่ปรากฏใน Callback 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 รางวัล
เมื่อนำเสนอโฆษณาของคุณ คุณต้องให้การเรียกกลับเพื่อจัดการรางวัลสำหรับ
ผู้ใช้รายนั้น โฆษณาจะแสดงได้ 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 ใช้รูปแบบโฆษณาทั้งหมดเพียงเล็กน้อย