---
metadata:
  - name: generator
    content: Diplodoc Platform v5.52.0
alternate:
  - https://ads.yandex.com/helpcenter/en/easy/integration/unity/formats/interstitial.md
  - https://ads.yandex.com/helpcenter/ru/easy/integration/unity/formats/interstitial.md
  - https://ads.yandex.com/helpcenter/zh/easy/integration/unity/formats/interstitial.md
  - href: zh/easy/integration/unity/formats/interstitial.md
    type: text/markdown
    title: Markdown version
  - href: ../llms.txt
    type: text/markdown
    title: llms.txt
---
> **Documentation Index:** Fetch the complete configuration index at https://ads.yandex.com/helpcenter/zh/llms.txt

# 插屏广告

<!-- source: zh/easy/_includes/sdk-guide.md -->
插屏广告是一种全屏广告格式，会在自然停顿处（例如当用户进入游戏下一关卡或完成转化操作时）嵌入到应用内容中。
<!-- endsource: zh/easy/_includes/sdk-guide.md -->

<!-- source: zh/easy/_includes/notes-unity-easy-guide.md -->
{% note info %}

[演示项目](https://ads.yandex.com/helpcenter/zh/easy/integration/unity/testing.md#demo)中提供了展示所有格式类型运作原理的示例。

{% endnote %}
<!-- endsource: zh/easy/_includes/notes-unity-easy-guide.md -->

#|
||

**实体**

|

**描述**

||
||

`HandleAdFailedToLoad`

|

如果 `HandleAdFailedToLoad()` 返回错误，请勿尝试**使用相同方法**再次加载新广告。

||
||

`adUnitId`

|

用途：

- **开发模式**，用于配合[演示广告单元](https://ads.yandex.com/helpcenter/zh/easy/integration/unity/testing.md#blocks)使用。

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

||
|#


## 插屏广告创建示例

```C#
using UnityEngine;
using UnityEngine.Events;
using YandexMobileAds;
using YandexMobileAds.Base;
using System.Collections.Generic;
using System;

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

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

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

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

    private Interstitial interstitial;
    private InterstitialAdLoader interstitialAdLoader;

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

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

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

    [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;

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

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

    private void SetupLoader()
    {
        interstitialAdLoader = new InterstitialAdLoader();
        interstitialAdLoader.OnAdLoaded += HandleAdLoaded;
        interstitialAdLoader.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
        {
            interstitialAdLoader.LoadAd(adRequestConfiguration);
        }
        catch (Exception ex)
        {
            Debug.LogError($"Configuration failed: {ex.Message}");
        }
    }

    public void Load()
    {
        ConfigureAd();
    }

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

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

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

    #region Event Handlers

    private void HandleAdLoaded(object sender, InterstitialAdLoadedEventArgs args)
    {
        interstitial = args.Interstitial;

        interstitial.OnAdShown += HandleAdShown;
        interstitial.OnAdDismissed += HandleAdDismissed;
        interstitial.OnAdClicked += HandleAdClicked;
        interstitial.OnAdFailedToShow += HandleAdFailedToShow;
        interstitial.OnAdImpression += HandleImpression;

        OnAdLoaded?.Invoke(interstitial);

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

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

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

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

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

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

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

    #endregion
}

```


## 检查集成

<!-- source: zh/easy/_includes/notes-unity-easy-guide.md -->
创建并运行您的项目。您可以通过在 **Android Studio** 的 **Logcat** 中搜索 `YandexAds` 关键字来检查集成是否成功：
<!-- endsource: zh/easy/_includes/notes-unity-easy-guide.md -->

```
[Integration] Ad type interstitial was integrated successfully
```
