Di chuyển SDK

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 sử dụng phương thức LoadAd() ở cấp thực thể để tải quảng cáo, còn quảng cáo xen kẽ có tặng thưởng và quảng cáo khi mở ứng dụng sử dụng 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. Dưới đâ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) sử dụng phương thức IsLoaded(). Phương thức này trả về true nếu quảng cáo được tải. Do thay đổi về cách tải quảng cáo, trong phiên bản 8, bạn sẽ không thể 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. Điều này khiến phương thức IsLoaded() trở nên lỗi thời.

Phiên bản 8 có một phương thức mới là CanShowAd(). Phương thức này trả về true nếu quảng cáo vẫn có thể hiển thị. Dưới đây là ví dụ về cách sử dụng CanShowAd() đối với 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 có phương thức Show() có một sự kiện OnUserEarnedReward riêng biệt để xử lý các tín hiệu phần thưởng của người dùng, còn quảng cáo xen kẽ có tặng thưởng có một phương thức Show(Action<Reward>) với lệnh gọi lại để xử lý tín hiệu phần thưởng của người dùng. Trong phiên bản 8, các định dạng quảng cáo xen kẽ có tặng thưởng và có tặng thưởng sẽ sử dụng phương thức Show(Action<Reward>) với lệnh gọi lại để xử lý thông báo về phần thưởng cho 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 thực thể đại diện sự kiện. Trong phiên bản 8, chúng tôi đã sử dụng thực thể đại diện chung cho các sự kiện quảng cáo. Giờ đây, các sự kiện sẽ hiển thị 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 các đị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 để đảm bảo tính nhất quán cho các định dạng quảng cáo.

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
v7v8
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
Hiển thị() 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.