# 迁移到 Unity 的插页式广告单元 API

> 通过初始化 SDK、定义广告格式以及使用广告单元 ID 加载和显示插页式广告服务，过过渡 LevelPlay 插页式广告单元 API。

本指南介绍如何从当前实现过过渡 LevelPlay 插页式 API（使用广告单元 ID），以加载和显示插页式广告服务。

## 先决条件##prerequisites

* 支持的最低 SDK 为 8.6.0。您可以在[此处](/grow/levelplay/sdk/unity/package-integration.md)下载最新的 SDK。
* 确保使用 LevelPlay Initialization API 初始化 SDK。
* 在 LevelPlay 后台中找到 AdUnitID。在[此处](/grow/levelplay/platform/get-started/ad-units.md)了解更多信息。

## **创建插页式广告对象**##**create-interstitial-ad-object**

必须在收到 **OnInitSuccess** 回调后执行插页式广告对象的创建。

该对象是可重用的实例，可以处理多个加载并在整个会话中显示。创建后，应将其用于加载和展示同一广告单元的广告服务。

对于更高级的实现，如果需要，可以创建多个插页式广告对象。

```csharp
// Create interstitial ad object
interstitialAd = new LevelPlayInterstitialAd(interstitialAdUnitId);
```

## 注册到 Interstitial 事件##register-to-interstitial-events

在代码中实现 **LevelPlayInterstitialAdListener** 而不是 **LevelPlayInterstitialListener**，从而了解广告投放情况。 

* 建议在加载插页式广告之前设置监听器。
* 每个插页式广告都应有自己的监听器实现。
* 回调在主线程上运行。

```csharp
// Register to interstitial events
interstitialAd.OnAdLoaded += InterstitialOnAdLoadedEvent;
interstitialAd.OnAdLoadFailed += InterstitialOnAdLoadFailedEvent;
interstitialAd.OnAdDisplayed += InterstitialOnAdDisplayedEvent;
interstitialAd.OnAdDisplayFailed += InterstitialOnAdDisplayFailedEvent;
interstitialAd.OnAdClicked += InterstitialOnAdClickedEvent;
interstitialAd.OnAdClosed += InterstitialOnAdClosedEvent;
interstitialAd.OnAdInfoChanged += InterstitialOnAdInfoChangedEvent;

// Implement the events
void InterstitialOnAdLoadedEvent(LevelPlayAdInfo adInfo) {}
void InterstitialOnAdLoadFailedEvent(LevelPlayAdError error) {}
void InterstitialOnAdDisplayedEvent(LevelPlayAdInfo adInfo) {}
void InterstitialOnAdDisplayFailedEvent(LevelPlayAdInfo adInfo, LevelPlayAdError error) {}
void InterstitialOnAdClickedEvent(LevelPlayAdInfo adInfo) {}
void InterstitialOnAdClosedEvent(LevelPlayAdInfo adInfo) {}
void InterstitialOnAdInfoChangedEvent(LevelPlayAdInfo adInfo) {}
```

|     | 旧版                           | 广告单元（新）                         |
| --- | ---------------------------- | ------------------------------- |
| 监听器 | IronSourceInterstitialEvents | LevelPlayInterstitialAdListener |
| 回调  | OnAdReady                    | OnAdLoaded                      |
|     | OnAdLoadFailed               | OnAdLoadFailed                  |
|     | OnAdOpened                   | OnAdDisplayed                   |
|     | OnAdClosed                   | OnAdClosed                      |
|     | OnAdShowFailed               | OnAdDisplayFailed               |
|     | OnAdClicked                  | OnAdClicked                     |
|     | OnAdShowSucceeded            | -（已弃用）                          |
|     | -                            | OnAdInfoChanged                 |

### LevelPlay 广告信息##levelplay-ad-info

间歇监听器回调返回的 **AdInfo** 类已被 **LevelPlayAdInfo** 替换。在[此处](/grow/levelplay/sdk/unity/levelplay-listener-adinfo-integration.md)了解更多信息。

## 加载 Interstitial Ad##load-interstitial-ad

要加载插页式广告，请使用 **LoadAd** 而不是 **LoadInterstitial**。

```csharp
// Load interstitial ad
interstitialAd.LoadAd();
```

## 显示插页式广告##show-interstitial-ad

使用 **LevelPlayInterstitialAdListener** API 在收到 **OnAdLoaded** 回调后显示插页式广告。

* 如果使用放置，请在 **ShowAd** API 中通道放置/位置名称，如下文 Placements 部分所示。
* 广告成功展示给用户后，可以通过重复加载步骤加载另一个广告。

```csharp
// Show ad without placement
interstitialAd.ShowAd();
// Show ad with placement
interstitialAd.ShowAd(placementName: "placementName");
```

### 检查 Ad is Ready##check-ad-is-ready

为了避免展示失败，并确保广告可以正确展示，建议在调用 **ShowAd** API 之前使用以下 API。

**IsAdReady** – 如果广告已成功加载并且广告单元未设置上限，则返回 true，否则返回 false。

**IsPlacementCapped** – 有效放置/位置被限制时返回 true。如果放置/位置无效或未设置上限，此 API 将返回 false。

```csharp
// Check that ad is ready and that the placement is not capped
if (interstitialAd.IsAdReady() && !LevelPlayInterstitialAd.IsPlacementCapped(placementName))
{
    interstitialAd.ShowAd(placementName);
}
```

### 广告位##placements

我们支持在 LevelPlay 控制面板上设置插页的[广告位](/grow/levelplay/platform/settings/placements.md)节奏和上限。 

如果为插页式广告服务设置了位置，请调用 **ShowAd** 方法为指定的放置/位置提供广告。

```objective-c
// Check that ad is ready and that the placement is not capped
if (interstitialAd.IsAdReady() && !LevelPlayInterstitialAd.IsPlacementCapped(placementName))
{ 
    interstitialAd.ShowAd(placementName); 
}
```

## 多个广告单元插页 API##multiple-ad-unit-interstitial-apis

|     | 旧版                            | 广告单元（新）                 |
| --- | ----------------------------- | ----------------------- |
| 类   | IronSource                    | LevelPlayInterstitialAd |
| API | LoadInterstitial              | LoadAd                  |
|     | ShowInterstitial              | ShowAd                  |
|     | IsInterstitialPlacementCapped | IsPlacementCapped       |
|     | IsInterstitialReady           | IsAdReady               |

## **插页式广告服务的完整实现示例**##**full-implementation-example-of-interstitial-ads**

```csharp
public class InterstitialAdSample {
    私有变量 LevelPlayInterstitialAd interstitialAd;
    void CreateInterstitialAd() {
        // Create InterstitialAd instance
        interstitialAd = new LevelPlayInterstitialAd("interstitialAdUnitId");

        // Subscribe InterstitialAd events
        interstitialAd.OnAdLoaded += InterstitialOnAdLoadedEvent;
        interstitialAd.OnAdLoadFailed += InterstitialOnAdLoadFailedEvent;
        interstitialAd.OnAdDisplayed += InterstitialOnAdDisplayedEvent;
        interstitialAd.OnAdDisplayFailed += InterstitialOnAdDisplayFailedEvent;
        interstitialAd.OnAdClicked += InterstitialOnAdClickedEvent;
        interstitialAd.OnAdClosed += InterstitialOnAdClosedEvent;
        interstitialAd.OnAdInfoChanged += InterstitialOnAdInfoChangedEvent;
    }
    void LoadInterstitialAd() {
        // Load or reload InterstitialAd
        interstitialAd.LoadAd();
    }
    void ShowInterstitialAd() {
        // Show InterstitialAd, check if the ad is ready before showing
        if (interstitialAd.IsAdReady()) {
            interstitialAd.ShowAd();
        }
    }
    void DestroyInterstitialAd() {
        // Destroy InterstitialAd
        interstitialAd.DestroyAd();
    }
    // Implement InterstitialAd events
    void InterstitialOnAdLoadedEvent(LevelPlayAdInfo adInfo) { }
    void InterstitialOnAdLoadFailedEvent(LevelPlayAdError error) { }
    void InterstitialOnAdClickedEvent(LevelPlayAdInfo adInfo) { }
    void InterstitialOnAdDisplayedEvent(LevelPlayAdInfo adInfo) { }
    void InterstitialOnAdDisplayFailedEvent(LevelPlayAdInfo adInfo, LevelPlayAdError error) { }
    void InterstitialOnAdClosedEvent(LevelPlayAdInfo adInfo) { }
    void InterstitialOnAdInfoChangedEvent(LevelPlayAdInfo adInfo) { }
}
```

## LevelPlay Mediation 演示应用程序##levelplay-mediation-demo-app

Integration Demo 应用程序演示了如何在 app 中集成插页式广告单元 API。

[下载 Unity Demo 应用程序](https://github.com/ironsource-mobile/Mediation-Demo-Apps)
