> For the complete documentation index, see [llms.txt](https://one-admax-organization.gitbook.io/one-admax-sdk/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://one-admax-organization.gitbook.io/one-admax-sdk/oneadmax_dev_guide_en/unityplugin/interstitial.md).

# 4-2. Interstitial Ads for Unity

## Before you begin

{% content-ref url="/pages/E4sVveRS1O07LDkxrZrf" %}
[04. Unity-Plugin Guide](/one-admax-sdk/oneadmax_dev_guide_en/unityplugin.md)
{% endcontent-ref %}

## ONEAdMax SDK initialization

Before loading ads, it is necessary to initialize ONEAdMax.\
The `ONEAdMaxClient.Initialize()` should only be performed once during the initial setup.

```csharp
...
using ONEAdMax;
...
public class ONEAdMaxDemo : MonoBehaviour
{
    private static bool _isInitialized = false;
    
    void Start()
    {
        if (!_isInitialized)
        {
            // Initialize the ONEAdMax SDK.
            ONEAdMaxClient.Initialize(() =>
            {
                // This callback is called once the ONEAdMax SDK is initialized.
                _isInitialized = true
            });
        }
    }
}
```

## Interstitial Ads Sample Code

You can create an instance of `OAMInterstitial` and extend its functionality by handling events.

### Creating an instance of Interstitial Ads.

```csharp
public class InterstitialAdController : MonoBehaviour
{   
    OAMInterstitial _interstitialAd;
    
    /// <summary>
    /// Creates the ad.
    /// </summary>
    public void CreateInterstitialAd()
    {
        Debug.Log("Creating Interstitial ad.");
        
        if (_interstitialAd != null)
        {
            Debug.LogWarning("Already have a Interstitial ad.");
            return;
        }

        _interstitialAd = new OAMInterstitial();
    }
}
```

### Custom options for Interstitial Ads (optional).&#x20;

```csharp
/// <summary>
/// Customization of the ads screen.
/// </summary>
/// <seealso cref="CustomExtraKey"/>
/// <param name="ad"><see cref="OAMInterstitial"/></param>
private void SetCustomExtras(OAMInterstitial ad)
{
    var customExtras = new Dictionary<CustomExtraKey, object>
    {
        // Change interstitial ad background color and transparency (Defaults to "#FF000000")
        { CustomExtraKey.BACKGROUND_COLOR, "#FF000000" },

        // Set whether to show a close button in the top right corner of the interstitial ad. (Defaults to false)
        { CustomExtraKey.HIDE_CLOSE_BTN, false },

        // Change to False to provide a margin based on the ad image. (Defaults to true)
        { CustomExtraKey.CLOSE_BTN_MARGIN_FROM_EDGE, true },
        // Left margin of the interstitial ad close button. (Defaults to -28dp)
        { CustomExtraKey.CLOSE_BTN_LEFT_MARGIN, -28 },
        // Top margin of the interstitial ad close button. (Defaults to 20dp)
        { CustomExtraKey.CLOSE_BTN_TOP_MARGIN, 20 },
        // Right margin of the interstitial ad close button. (Defaults to 20dp)
        { CustomExtraKey.CLOSE_BTN_RIGHT_MARGIN, 20 },
        // Bottom margin of the interstitial ad close button. (Defaults to 0dp)
        { CustomExtraKey.CLOSE_BTN_BOTTOM_MARGIN, 0 },
        
        // Disable interstitial ad exit with a backkey. (Defaults to false)
        { CustomExtraKey.DISABLE_BACK_BTN, false },

        // Whether to show an exit message in interstitial ads. (Defaults to false)
        { CustomExtraKey.IS_ENDING_AD, false },
        // Change the exit ad message.
        { CustomExtraKey.ENDING_TEXT, "To exit, press the Back button one more time." },
        // Change the text size of the exit ad message.
        { CustomExtraKey.ENDING_TEXT_SIZE, 14 },
        // Change the text color of the exit ad message.
        { CustomExtraKey.ENDING_TEXT_COLOR, "#FFFFFFFF" },
        // Change the gravity of exit ad message.
        { CustomExtraKey.ENDING_TEXT_GRAVITY, OAMGravity.RIGHT },
    };

    ad.SetCustomExtras(customExtras);
}
```

### Setting an event listener for Interstitial Ads (optional).

```csharp
/// <summary>
/// Register to events the interstital ad may raise.
/// </summary>
private void RegisterEvents(OAMInterstitial ad)
{
    // Raised when an ad is loaded into the interstitial ad.
    ad.OnLoaded += () =>
    {
        Debug.Log("Interstitial ad loaded.");
    };

    // Raised when the interstitial ad fails to load.
    ad.OnLoadFailed += (OAMError error) =>
    {
        Debug.LogError("Interstitial ad failed to load an ad with error : " + error);
    };

    // Raised when an ad opened full screen content.
    ad.OnOpened += () =>
    {
        Debug.Log("Interstitial ad has been opened.");
    };

    // Raised when the ad failed to open full screen content.
    ad.OnOpenFailed += (OAMError error) =>
    {
        Debug.LogError("Interstitial ad failed to open an ad with error : " + error);
    };

    // Raised when the ad closed full screen content.
    ad.OnClosed += (CloseEventType closeEventType) =>
    {
        Debug.LogError("Interstitial ad closed with type : " + closeEventType);
    };

    // Raised when a click is recorded for an ad.
    ad.OnClicked += () =>
    {
        Debug.Log("Interstitial ad was clicked.");
    };
}
```

{% content-ref url="/pages/i8O1iX5eJtrY4LjZBdFC" %}
[ONE AdMax SDK Error Codes](/one-admax-sdk/oneadmax_dev_guide_en/oamerror.md)
{% endcontent-ref %}

### Creating Interstitial Ads&#x20;

Complete all the settings and create the **OAMInterstitial**.

```csharp
// These ad units are configured to always serve test ads.   
private readonly string _placementId = "string your placement ID;"

_interstitialAd.Create(_placementId);
```

### Interstitial Ads Load

Call the **`Load()`** API for interstitial ads to request an ad from the server.

```csharp
/// <summary>
/// Loads the ad.
/// </summary>
public void Load()
{
    if (_interstitialAd == null)
    {
        Debug.LogWarning("The interstital ad is null.");
        return;
    }

    _interstitialAd.Load();
}
```

{% hint style="warning" %}
Excessive ad requests can be a reason for blocking, so please be cautious!
{% endhint %}

### Interstitial Ads 노출하기

광고 로드가 완료되면 광고를 노출을 원하는 시점에 `Show()` API를 호출하여 화면에 노출 시킵니다.

```csharp
/// <summary>
/// Shows the ad.
/// </summary>
public void Show()
{
    if (_interstitialAd != null && _interstitialAd.IsLoaded())
    {
        Debug.Log("Showing interstital ad.");
        _interstitialAd.Show();
    }
    else
    {
        Debug.LogError("The Interstitial ad hasn't loaded yet.");
    }
}
```

### Interstitial Ads Memory Destroy

Destroy the memory within the instance of the interstitial ad.

```csharp
/// <summary>
/// Destroys the ad.
/// </summary>
public void Destroy()
{
    if (_interstitialAd != null)
    {
        Debug.Log("Destroying interstitial ad.");
        _interstitialAd.Destroy();
        _interstitialAd = null;
    }
}
```

### Check whether the Interstitial Ads are loaded.

Check whether the interstitial ad is loaded. (returns **true** | **false**)

```csharp
_interstitialAd.IsLoaded();
```
