# 在 Unity 中实现插页式广告

> 在 Unity 项目中实现插页式广告。加载广告内容，并通过 C# 脚本在游戏间隙或应用程序切换期间展示广告。

要使用 [`Advertisements API`](/grow/ads/unity-sdk/unity-api.md) 来展示全屏插页式广告，请初始化 SDK，然后使用 [`Load`](/grow/ads/unity-sdk/unity-api.md#load) 函数将广告内容加载到[广告单元](/grow/dashboard/ad-units.md)中，并使用 [`Show`](/grow/ads/unity-sdk/unity-api.md#show) 函数展示广告。

> **Note:**
>
> 从 SDK 4.4.1 版开始，Unity Ads 包在 Unity 编辑器中已更名为 Advertisement Legacy 包。
> Advertisement Legacy 包 4.12 版仍能正常工作，但不会收到任何关于新功能或增强功能的进一步更新。
> SDK 4.12 版支持 Apple 隐私清单更新，无计划对该包进一步更新。

在 SDK 3.7.0 及更高版本中，当内容成功或失败地加载或显示时，可以使用 [`IUnityAdsLoadListener`](/grow/ads/unity-sdk/unity-api.md) 和 [`IUnityAdsShowListener`](/grow/ads/unity-sdk/unity-api.md) 回调分别实现相应逻辑。

> **Note:**
>
> 调用 `Show` 方法时，必须指定 Placement ID（广告位 ID），否则将发生错误。

## 插页式广告示例##interstitial-ad-example

1. **C#**

   ```cs
   using UnityEngine;
   using UnityEngine.Advertisements;

   public class InterstitialAdExample : MonoBehaviour, IUnityAdsLoadListener, IUnityAdsShowListener
   {
     [SerializeField] string _androidAdUnitId = "Interstitial_Android";
     [SerializeField] string _iOSAdUnitId = "Interstitial_iOS";
     string _adUnitId;

     void Awake()
     {
       // Get the Ad Unit ID for the current platform:
       _adUnitId = (Application.platform == RuntimePlatform.IPhonePlayer)
       ? _iOSAdUnitId
       : _androidAdUnitId;
     }

     // Load content to the Ad Unit:
     public void LoadAd()
     {
       // IMPORTANT!仅在初始化完成后加载内容（在此示例中，初始化在另一个脚本中处理）。
       Debug.Log("Loading Ad: " + _adUnitId);
       Advertisement.Load(_adUnitId, this);
     }

     // Show the loaded content in the Ad Unit:
     public void ShowAd()
     {
       // Note that if the ad content wasn't previously loaded, this method will fail
       Debug.Log("Showing Ad: " + _adUnitId);
       Advertisement.Show(_adUnitId, this);
     }

     // Implement Load Listener and Show Listener interface methods:
     public void OnUnityAdsAdLoaded(string adUnitId)
     {
       // Optionally execute code if the Ad Unit successfully loads content.
     }

     public void OnUnityAdsFailedToLoad(string _adUnitId, UnityAdsLoadError error, string message)
     {
       Debug.Log($"Error loading Ad Unit: {_adUnitId} - {error.ToString()} - {message}");
       // Optionally execute code if the Ad Unit fails to load, such as attempting to try again.
     }

     public void OnUnityAdsShowFailure(string _adUnitId, UnityAdsShowError error, string message)
     {
       Debug.Log($"Error showing Ad Unit {_adUnitId}: {error.ToString()} - {message}");
       // Optionally execute code if the Ad Unit fails to show, such as loading another ad.
     }

     public void OnUnityAdsShowStart(string _adUnitId) { }
     public void OnUnityAdsShowClick(string _adUnitId) { }
     public void OnUnityAdsShowComplete(string _adUnitId, UnityAdsShowCompletionState showCompletionState) { }
   }
   ```

**后续步骤**：请参阅关于[在 Unity 中实现奖励广告](/grow/ads/unity-sdk/rewarded-ads.md)的文档以改进您的实现。
