激励广告

激励广告是一种全屏广告格式,用户观看广告可获得奖金或其他奖励(例如游戏币、重试次数、高级功能使用权等)。

备注

用户可决定是否观看广告或跳过广告,只有观看完广告才能获得奖励。

备注

演示项目中提供了展示所有格式类型运作原理的示例。

实体

描述

adUnitId

用途:

  • 开发模式,用于配合演示广告单元使用。

  • 生产模式,用于配合 R-M-XXXXXX-Y 使用(实际 ID 请在 Yandex Advertising Network 界面查询)。R-M-XXXXXX-Y 是您实际广告单元 ID 的模板,将用于接收各种广告创意。

激励广告创建示例

using UnityEngine;
using UnityEngine.Events;
using YandexMobileAds;
using YandexMobileAds.Base;
using System.Collections.Generic;
using System;

[AddComponentMenu("Yandex Ads/Rewarded Ad Component")]
public class RewardedAdComponent : MonoBehaviour
{
    [Header("Ad Settings")]
    public string adName = "Default Rewarded Ad";

    [Header("Ad Unit IDs")]
    [Tooltip("Ad Unit ID for Android")]
    public string adUnitIdAndroid = "demo-rewarded-yandex";

    [Tooltip("Ad Unit ID for iOS")]
    public string adUnitIdiOS = "demo-rewarded-yandex";

    [Header("Configuration")]
    public bool autoLoading = true;
    public bool showAfterLoading = true;

    private RewardedAd rewardedAd;
    private RewardedAdLoader rewardedAdLoader;

    [System.Serializable]
    public class AdEvent : UnityEvent { }

    [System.Serializable]
    public class AdEventString : UnityEvent<string> { }

    [System.Serializable]
    public class AdEventWithAd : UnityEvent<RewardedAd> { }

    [System.Serializable]
    public class AdEventRewared : UnityEvent<Reward> { }

    [Header("Load Callbacks")]
    public AdEventWithAd OnAdLoaded;
    public AdEventString OnAdFailedToLoad;

    [Header("Interaction Callbacks")]
    public AdEventWithAd OnAdShown;
    public AdEventWithAd OnAdDismissed;
    public AdEventWithAd OnAdClicked;
    public AdEventString  OnAdFailedToShow;
    public AdEventString OnImpression;
    public AdEventRewared OnRewarded;

    private string CurrentAdUnitId
    {
        get
        {
#if UNITY_IOS
            return adUnitIdiOS;
#elif UNITY_ANDROID
            return adUnitIdAndroid;
#else
            Debug.LogWarning("Unsupported platform for Yandex Ads. Using IOS Ad Unit ID by default.");
            return adUnitIdAndroid;
#endif
        }
    }

    private void Start()
    {
        if (!string.IsNullOrEmpty(CurrentAdUnitId))
        {
            SetupLoader();
            if (autoLoading)
            {
                ConfigureAd();
            }
            DontDestroyOnLoad(gameObject);
        }
        else
        {
            Debug.Log("RewardedAdComponent: Ad Unit ID is missing.");
        }
    }

    private void SetupLoader()
    {
        rewardedAdLoader = new RewardedAdLoader();
        rewardedAdLoader.OnAdLoaded += HandleAdLoaded;
        rewardedAdLoader.OnAdFailedToLoad += HandleAdFailedToLoad;
    }

    private void ConfigureAd()
    {
        double lat = 60.0, lon = 30.0;

        // 构建位置对象
        Location location = new Location.Builder()
            .SetLatitude(lat)
            .SetLongitude(lon)
            .Build();

        AdRequestConfiguration adRequestConfiguration = new AdRequestConfiguration.Builder(CurrentAdUnitId)
                .WithAge("25")
                .WithGender("male") // 或 "female"、"other"
                .WithContextTags(new List<string>() { "games", "unity", "test" })
                .WithContextQuery("user_search_query") // 搜索查询字符串
                .WithLocation(location)
                .WithParameters(new Dictionary<string, string> {
                        { "custom1", "value1" },
                        { "custom2", "value2" }
                }).Build();
        try
        {
            rewardedAdLoader.LoadAd(adRequestConfiguration);
        }
        catch (Exception ex)
        {
            Debug.LogError($"{adName}: Configuration failed: {ex.Message}");
        }
    }

    public void Load()
    {
        ConfigureAd();

    }

    public void Show()
    {
        if (rewardedAd != null)
        {
            rewardedAd.Show();
        }
        else
        {
            Debug.Log("Failed to show ad. Ad object not loaded");
        }
    }

    public void OnDestroy()
    {
        if (rewardedAd != null)
        {
            rewardedAd.OnAdShown -= HandleAdShown;
            rewardedAd.OnAdDismissed -= HandleAdDismissed;
            rewardedAd.OnAdClicked -= HandleAdClicked;
            rewardedAd.OnAdFailedToShow -= HandleAdFailedToShow;
            rewardedAd.OnAdImpression -= HandleImpression;
            rewardedAd.OnRewarded-= HandleRewarded;
            rewardedAd.Destroy();
            rewardedAd = null;
        }

        if (rewardedAdLoader != null)
        {
            rewardedAdLoader.OnAdLoaded -= HandleAdLoaded;
            rewardedAdLoader.OnAdFailedToLoad -= HandleAdFailedToLoad;
            rewardedAdLoader = null;
        }
    }

    #region Event Handlers

    private void HandleAdLoaded(object sender, RewardedAdLoadedEventArgs args)
    {
        rewardedAd = args.RewardedAd;

        rewardedAd.OnAdShown += HandleAdShown;
        rewardedAd.OnAdDismissed += HandleAdDismissed;
        rewardedAd.OnAdClicked += HandleAdClicked;
        rewardedAd.OnAdFailedToShow += HandleAdFailedToShow;
        rewardedAd.OnAdImpression += HandleImpression;
        rewardedAd.OnRewarded += HandleRewarded;

        OnAdLoaded?.Invoke(rewardedAd);

        if (showAfterLoading)
        {
            rewardedAd.Show();
        }
    }

    private void HandleAdFailedToLoad(object sender, AdFailedToLoadEventArgs args) => OnAdFailedToLoad?.Invoke(args.Message);

    private void HandleAdShown(object sender, EventArgs args) => OnAdShown?.Invoke(rewardedAd);

    private void HandleAdDismissed(object sender, EventArgs args) => OnAdDismissed?.Invoke(rewardedAd);

    private void HandleAdClicked(object sender, EventArgs args) => OnAdClicked?.Invoke(rewardedAd);

    private void HandleAdFailedToShow(object sender, AdFailureEventArgs args) => OnAdFailedToShow?.Invoke(args.Message);

    private void HandleImpression(object sender, ImpressionData impressionData) =>
        OnImpression?.Invoke(impressionData?.rawData ?? string.Empty);

    private void HandleRewarded(object sender, Reward args) => OnRewarded?.Invoke(args);

    #endregion
}

检查集成

创建并运行您的项目。您可以通过在 Android StudioLogcat 中搜索 YandexAds 关键字来检查集成是否成功:

[Integration] Ad type rewarded was integrated successfully
上一篇
下一篇