Lần cập nhật gần đây nhất: Tháng 2 năm 2023
Trang này trình bày cách di chuyển đối với phiên bản hiện tại và phiên bản cũ.
Chuyển từ phiên bản 7 sang phiên bản 8
Các định dạng toàn màn hình hiện sử dụng phương thức tải tĩnh
Trong phiên bản 7 của trình bổ trợ, quảng cáo xen kẽ và quảng cáo có tặng thưởng có một cấp thực thể
Phương thức LoadAd()
để tải một quảng cáo trong khi quảng cáo xen kẽ có tặng thưởng và ứng dụng
quảng cáo mở có phương thức Load()
tĩnh để tải quảng cáo. Trong phiên bản 8, tất cả các định dạng quảng cáo toàn màn hình (quảng cáo xen kẽ, quảng cáo có tặng thưởng, quảng cáo xen kẽ có tặng thưởng và quảng cáo khi mở ứng dụng) sẽ sử dụng phương thức Load()
tĩnh để tải quảng cáo. Sau đây là ví dụ về cách
để tải một quảng cáo xen kẽ:
Phiên bản 8 (Hiện tại)
#if UNITY_ANDROID
const string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
const string adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
const string adUnitId = "unexpected_platform";
#endif
private InterstitialAd _interstitialAd;
private void LoadAd()
{
// Load an interstitial ad
InterstitialAd.Load(adUnitId, new AdRequest(),
(InterstitialAd ad, LoadAdError loadAdError) =>
{
if (loadAdError != null)
{
Debug.Log("Interstitial ad failed to load with error: " +
loadAdError.GetMessage());
return;
}
else if (ad == null)
{
Debug.Log("Interstitial ad failed to load.");
return;
}
Debug.Log("Interstitial ad loaded.");
_interstitialAd = ad;
});
}
Phiên bản 7 (Cũ)
#if UNITY_ANDROID
const string adUnitId = "ca-app-pub-3940256099942544/1033173712";
#elif UNITY_IPHONE
const string adUnitId = "ca-app-pub-3940256099942544/4411468910";
#else
const string adUnitId = "unexpected_platform";
#endif
private InterstitialAd _interstitialAd;
private void LoadInterstitialAd()
{
// Initialize an InterstitialAd.
_interstitialAd = new InterstitialAd(adUnitId);
// Called when an ad request has successfully loaded.
_interstitialAd.OnAdLoaded += HandleOnAdLoaded;
// Called when an ad request has failed to load.
_interstitialAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
_interstitialAd.LoadAd(request);
}
private void HandleOnAdLoaded(object sender, EventArgs args)
{
Debug.Log("Interstitial ad loaded.");
}
private void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
if (args != null)
{
Debug.Log("Interstitial ad failed to load with error: " +
args.LoadAdError.GetMessage());
}
}
Sau đây là ví dụ về cách tải một quảng cáo có tặng thưởng:
Phiên bản 8 (Hiện tại)
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
const string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
const string adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
const string adUnitId = "unused";
#endif
private RewardedAd _rewardedAd;
private void LoadRewardedAd()
{
// Load a rewarded ad
RewardedAd.Load(adUnitId, new AdRequest(),
(Rewarded ad, LoadAdError loadError) =>
{
if (loadError != null)
{
Debug.Log("Rewarded ad failed to load with error: " +
loadError.GetMessage());
return;
}
else if (ad == null)
{
Debug.Log("Rewarded ad failed to load.");
return;
}
Debug.Log("Rewarded ad loaded.");
_rewardedAd = ad;
});
}
Phiên bản 7 (Cũ)
// These ad units are configured to always serve test ads.
#if UNITY_ANDROID
const string adUnitId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
const string adUnitId = "ca-app-pub-3940256099942544/1712485313";
#else
const string adUnitId = "unused";
#endif
private RewardedAd _rewardedAd;
private void LoadRewardedAd()
{
// Initialize an InterstitialAd.
_rewardedAd = new RewardedAd(adUnitId);
// Called when an ad request has successfully loaded.
_rewardedAd.OnAdLoaded += HandleOnAdLoaded;
// Called when an ad request has failed to load.
_rewardedAd.OnAdFailedToLoad += HandleOnAdFailedToLoad;
// Create an empty ad request.
AdRequest request = new AdRequest.Builder().Build();
// Load the interstitial with the request.
_rewardedAd.LoadAd(request);
}
private void HandleOnAdLoaded(object sender, EventArgs args)
{
Debug.Log("Rewarded ad loaded.");
}
private void HandleOnAdFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
if (args != null)
{
Debug.Log("Rewarded ad failed to load with error: " +
args.LoadAdError.GetMessage());
}
}
Sử dụng CanShowAd() để kiểm tra trạng thái sẵn sàng trong việc hiển thị quảng cáo toàn màn hình
Trong phiên bản 7, quảng cáo toàn màn hình (quảng cáo xen kẽ, quảng cáo có tặng thưởng, quảng cáo xen kẽ có tặng thưởng,
và quảng cáo khi mở ứng dụng) có phương thức IsLoaded()
trả về true
nếu quảng cáo
đã tải xong. Do thay đổi về cách tải quảng cáo, trong phiên bản 8, bạn sẽ không có
quyền truy cập vào đối tượng quảng cáo toàn màn hình cho đến khi quảng cáo được tải, giúp
Phương thức IsLoaded()
đã lỗi thời.
Phiên bản 8 có một phương thức mới tên là CanShowAd()
. Phương thức này trả về true
nếu quảng cáo
vẫn có thể được hiển thị. Dưới đây là ví dụ về cách sử dụng CanShowAd()
trên
quảng cáo xen kẽ:
Phiên bản 8 (Hiện tại)
private InterstitialAd _interstitalAd;
public void ShowInterstitialAd()
{
if (_interstitalAd != null && _interstitalAd.CanShowAd())
{
_interstitalAd.Show();
}
else
{
Debug.Log("Interstitial ad cannot be shown.");
}
}
Phiên bản 7 (Cũ)
private InterstitialAd _interstitalAd;
public void ShowInterstitialAd()
{
if (_interstitalAd != null && _interstitalAd.IsLoaded())
{
_interstitalAd.Show();
}
else
{
Debug.Log("Interstitial ad is not ready yet.");
}
}
Sử dụng Show(Action) để hiển thị quảng cáo có tặng thưởng
Trong phiên bản 7 của trình bổ trợ, quảng cáo có tặng thưởng sử dụng phương thức Show()
với một
OnUserEarnedReward
sự kiện để xử lý các tín hiệu về phần thưởng của người dùng, và
quảng cáo xen kẽ có tặng thưởng sử dụng phương thức Show(Action<Reward>)
với lệnh gọi lại cho
xử lý tín hiệu về phần thưởng của người dùng. Trong phiên bản 8, có tặng thưởng
định dạng quảng cáo xen kẽ sẽ có phương thức Show(Action<Reward>)
với một
lệnh gọi lại để xử lý thông báo về phần thưởng của người dùng.
Sau đây là ví dụ về cách hiển thị một quảng cáo có tặng thưởng:
Phiên bản 8 (Hiện tại)
private RewardedAd _rewardedAd;
public void ShowRewardedAd()
{
if (_rewardedAd != null && _rewardedAd.CanShowAd())
{
_rewardedAd.Show((Reward reward) =>
{
Debug.Log("Rewarded ad granted a reward: " +
reward.Amount);
});
}
else
{
Debug.Log("Rewarded ad cannot be shown.");
}
}
Phiên bản 7 (Cũ)
private RewardedAd _rewardedAd;
public void ShowRewardedAd()
{
if (_rewardedAd != null && _rewardedAd.CanShowAd())
{
_rewardedAd.OnUserEarnedReward += HandleUserEarnedReward;
_rewardedAd.Show());
}
else
{
Debug.Log("Rewarded ad is not ready yet.");
}
}
public void HandleUserEarnedReward(object sender, Reward reward)
{
Debug.Log("Rewarded ad granted a reward: " +
reward.Amount);
}
Thực thể đại diện sự kiện quảng cáo hiện áp dụng đối số cho loại cụ thể
Trong phiên bản 7 của API này, chúng tôi đã sử dụng
EventHandlers
khi xác định người được uỷ quyền tham gia sự kiện. Trong phiên bản 8, chúng tôi đã áp dụng chung
đại biểu
cho các sự kiện quảng cáo. Do đó, các sự kiện hiện sẽ phát trực tiếp giá trị sự kiện mà không được gói trong lớp EventArg
.
Dưới đây là ví dụ về cách sử dụng OnAdPaid
(thay cho OnPaidEvent
):
Phiên bản 8 (Hiện tại)
private BannerView _bannerView;
public void ConfigureBanner()
{
_bannerView.OnAdPaid += (AdValue value) =>
{
AdValue value = value;
};
}
Phiên bản 7 (Cũ)
private BannerView _bannerView;
public void ConfigureBanner()
{
_bannerView.OnPaidEvent += (object sender, AdValueEventArg arg) =>
{
AdValue value = arg.Value;
};
}
Các định dạng quảng cáo hiện tuân theo giao diện đồng nhất
Trong phiên bản 7 của trình bổ trợ, có sự khác biệt về tên sự kiện giữa định dạng quảng cáo toàn màn hình. Trong phiên bản 8, chúng tôi đã đổi tên nhiều phương thức API thành nhất quán trên các định dạng quảng cáo của chúng tôi.
Bảng sau đây liệt kê các thay đổi về lớp được ra mắt trong phiên bản 8.
BannerView | |
---|---|
phiên bản 7 | v8 |
OnAdLoaded | OnBannerAdLoaded |
OnAdFailedToLoad | OnBannerAdLoadFailed |
OnAdOpening | OnAdFullScreenContentOpened |
OnAdClosed | OnAdFullScreenContentClosed |
OnPaidEvent | OnAdPaid |
InterstitialAd | |
LoadAd() | InterstitialAd.Load() |
InterstitialAd() | InterstitialAd.Load() |
OnAdLoaded | InterstitialAd.Load() |
OnAdFailedToLoad | InterstitialAd.Load() |
OnAdOpening | OnAdFullScreenContentOpened |
OnAdClosed | OnAdFullScreenContentClosed |
OnAdFailedToShow | OnAdFullScreenContentFailed |
OnAdDidRecordImpression | OnAdImpressionRecorded |
OnPaidEvent | OnAdPaid |
RewardedAd | |
LoadAd() | RewardedAd.Load() |
RewardedAd() | RewardedAd.Load() |
OnAdLoaded | RewardedAd.Load() |
OnAdFailedToLoad | RewardedAd.Load() |
OnAdOpening | OnAdFullScreenContentOpened |
OnAdClosed | OnAdFullScreenContentClosed |
OnAdFailedToShow | OnAdFullScreenContentFailed |
OnAdDidRecordImpression | OnAdImpressionRecorded |
OnPaidEvent | OnAdPaid |
Show() | Show() |
OnUserEarnedReward | Show() |
RewardedInterstitialAd | |
LoadAd() | RewardedInterstitialAd.Load() |
OnPaidEvent | OnAdPaid |
OnAdDidPresentFullScreenContent | OnAdFullScreenContentOpened |
OnAdDidDismissFullScreenContent | OnAdFullScreenContentClosed |
OnAdFailedToPresentFullScreenContent | OnAdFullScreenContentFailed |
OnAdDidRecordImpression | OnAdImpressionRecorded |
AppOpenAd | |
LoadAd() | AppOpenAd.Load() |
OnPaidEvent | OnAdPaid |
OnAdDidPresentFullScreenContent | OnAdFullScreenContentOpened |
OnAdDidDismissFullScreenContent | OnAdFullScreenContentClosed |
OnAdFailedToPresentFullScreenContent | OnAdFullScreenContentFailed |
OnAdDidRecordImpression | OnAdImpressionRecorded |
AdErrorEventArgs | |
AdErrorEventArgs.AdError | Sử dụng trực tiếp AdError. |
AdFailedToLoadEventArgs | |
AdFailedToLoadEventArgs.LoadAdError | Sử dụng trực tiếp LoadAdError. |
AdValueEventArgs | |
AdValueEventArgs.AdValue | Sử dụng trực tiếp AdValue. |