# Unityのインタースティシャル広告単位APIへの移行

> SDK の初期化、広告フォーマットの定義、インタースティシャル広告のロードと表示のための広告単位 ID の使用によって、LevelPlay インタースティシャル広告単位 API に遷移。

このガイドでは、現在の実装から LevelPlay Interstitial API に遷移(広告単位 ID を使用)して、インタースティシャル広告をロードおよびディスプレイする方法について説明します。

## 前提条件##prerequisites

* サポートされている最小の SDK は 8.6.0 です。最新の SDK は[ここ](/grow/levelplay/sdk/unity/package-integration.md)からダウンロードできます。
* LevelPlay 初期化 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);
```

## インタースティシャルイベントに登録##register-to-interstitial-events

コードに **LevelPlayInterstitialAdListener** ではなく **LevelPlayInterstitial**AdListener を実装して、広告の配信を通知します。 

* インタースティシャル広告をロードする前にリスナーを設定することをお勧めします。
* 各インタースティシャル広告には、独自のリスナー実装が必要です。
* コールバックはメインスレッドで実行されます。

```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)を参照してください。

## ロード インタースティシャル広告##load-interstitial-ad

インタースティシャル広告をロードするには、**LoadInterstitial** ではなく **LoadAd** を使用します。

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

## インタースティシャル広告の表示##show-interstitial-ad

**LevelPlayInterstitialAdListener** API を使用し**て OnAdLoaded** コールバックを受信した後にインタースティシャル広告を表示します。

* プレースメントを使用している場合は、後述のプレースメントのセクションで示すように、**ShowAd** APIで配置名を渡します。
* ユーザーに広告が正常に表示されたら、ロードステップを繰り返して別の広告をロードできます。

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

### 広告の準備ができているか確認##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##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 {
    private 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

インテグレーション Demo (インテグレーションデモ) アプリケーションは、アプリケーションにインタースティシャル広告単位 API を統合する方法を示します。

[Unityデモ アプリケーションのダウンロード](https://github.com/ironsource-mobile/Mediation-Demo-Apps)
