Thưởng cho người chơi khi xem quảng cáo giúp tăng mức độ tương tác của người dùng, dẫn đến doanh thu cao hơn. Ví dụ: người chơi có thể được thưởng tiền trong trò chơi, vật phẩm tiêu hao, lượt chơi bổ sung hoặc hệ số kinh nghiệm. Để tìm hiểu thêm về cách thiết kế quảng cáo có thưởng sao cho hiệu quả, tham khảo tài liệu về Chiến lược kiếm tiền.
Để tặng thưởng cho người chơi vì đã xem hết video quảng cáo, hãy triển khai phương thức gọi lại bằng cách sử dụng kết quả ShowResult
để kiểm tra xem người dùng đã xem hết quảng cáo hay chưa và có được thưởng hay không.
Các nút quảng cáo có thưởng
Việc sử dụng nút để cho phép người chơi chọn xem quảng cáo là cách triển khai phổ biến đối với video quảng cáo có thưởng. Sử dụng mã ví dụ trong các bước sau để tạo nút quảng cáo có thưởng. Nút này sẽ hiển thị quảng cáo khi nhấn, miễn là có sẵn nội dung quảng cáo.
Để định cấu hình nút trong Unity Editor:
- Lựa chọn Đối tượng trò chơi > Giao diện người dùng > Nút để thêm một nút vào Cảnh của bạn.
- Chọn nút bạn đã thêm vào Cảnh của mình, sau đó thêm thành phần tập lệnh vào đó bằng Trình kiểm tra (Thêm thành phần> Tập lệnh mới). Đặt tên tập lệnh là
RewardedAdsButton
để khớp với tên lớp. - Mở tập lệnh và thêm mã sau:
using UnityEngine; using UnityEngine.UI; using UnityEngine.Advertisements; public class RewardedAdsButton : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener { [SerializeField] Button _showAdButton; [SerializeField] string _androidAdUnitId = "Rewarded_Android"; [SerializeField] string _iOSAdUnitId = "Rewarded_iOS"; string _adUnitId = null; // This will remain null for unsupported platforms void Awake() { // Get the Ad Unit ID for the current platform: #if UNITY_IOS _adUnitId = _iOSAdUnitId; #elif UNITY_ANDROID _adUnitId = _androidAdUnitId; #endif // Disable the button until the ad is ready to show: _showAdButton.interactable = false; } // Call this public method when you want to get an ad ready to show. public void LoadAd() { // IMPORTANT! Only load content AFTER initialization (in this example, initialization is handled in a different script). Debug.Log("Loading Ad: " + _adUnitId); Advertisement.Load(_adUnitId, this); } // If the ad successfully loads, add a listener to the button and enable it: public void OnUnityAdsAdLoaded(string adUnitId) { Debug.Log("Ad Loaded: " + adUnitId); if (adUnitId.Equals(_adUnitId)) { // Configure the button to call the ShowAd() method when clicked: _showAdButton.onClick.AddListener(ShowAd); // Enable the button for users to click: _showAdButton.interactable = true; } } // Implement a method to execute when the user clicks the button: public void ShowAd() { // Disable the button: _showAdButton.interactable = false; // Then show the ad: Advertisement.Show(_adUnitId, this); } // Implement the Show Listener's OnUnityAdsShowComplete callback method to determine if the user gets a reward: public void OnUnityAdsShowComplete(string adUnitId, UnityAdsShowCompletionState showCompletionState) { if (adUnitId.Equals(_adUnitId) && showCompletionState.Equals(UnityAdsShowCompletionState.COMPLETED)) { Debug.Log("Unity Ads Rewarded Ad Completed"); // Grant a reward. } } // Implement Load and Show Listener error callbacks: public void OnUnityAdsFailedToLoad(string adUnitId, UnityAdsLoadError error, string message) { Debug.Log($"Error loading Ad Unit {adUnitId}: {error.ToString()} - {message}"); // Use the error details to determine whether to try to load another ad. } public void OnUnityAdsShowFailure(string adUnitId, UnityAdsShowError error, string message) { Debug.Log($"Error showing Ad Unit {adUnitId}: {error.ToString()} - {message}"); // Use the error details to determine whether to try to load another ad. } public void OnUnityAdsShowStart(string adUnitId) { } public void OnUnityAdsShowClick(string adUnitId) { } void OnDestroy() { // Clean up the button listeners: _showAdButton.onClick.RemoveAllListeners(); } }
Bước tiếp theo: Tham khảo phần Triển khai banner quảng cáo trong Unity.