# 4-3. Banner Ads for Unity

## Before you begin

{% content-ref url="" %}
[](https://one-admax-organization.gitbook.io/one-admax-sdk/oneadmax_dev_guide_en/unityplugin)
{% endcontent-ref %}

## ONEAdMax SDK Intitialize

ONEAdMax initialization is required before loading ads.\
The method **ONEAdMaxClient.Initialize()** must be executed only once.

```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
            });
        }
    }
}
```

## Banner ADs Sample Code

You can create an **`OAMBannerView`**&#x69;nstance and handle lifecycle events to extend its functionality.

### Create a **Banner View** instance.

```csharp
using ONEAdMax;
...
public class BannerViewController : MonoBehaviour
{    
    OAMBannerView _bannerView;
    
    public void CreateBannerView()
    {
        Debug.Log("Creating banner view.");

        // If we already have a banner.
        if (_bannerView != null) {
            Debug.LogWarning("Already have a banner.");
            return;
        }

        // Create a 320x50 banner view at top of the screen.
        _bannerView = new OAMBannerView(AdSize.BANNER_320x50, AdPosition.Top);
    }
}
```

{% hint style="success" %}
The parameters for the **Banner View** constructor are:

* `AdSize`: The size of the ad you want to use.
* `AdPosition`: The position where the banner view should be placed.
  {% endhint %}

### Banner Size

<table><thead><tr><th>Size</th><th>상수(Constant)</th><th data-hidden>상수</th><th data-hidden></th></tr></thead><tbody><tr><td>320x50</td><td>AdSize.BANNER_320x50</td><td>BANNER_320x50</td><td></td></tr><tr><td>300x250</td><td>AdSize.BANNER_300x250</td><td>BANNER_300x250</td><td></td></tr><tr><td>320x100</td><td>AdSize.BANNER_320x100</td><td>BANNER_320x100</td><td></td></tr></tbody></table>

### Set the banner display animation.

Set the animation when the banner is displayed. (Default: **`AnimType.NONE`**)\
There are a total of 7 animations you can configure.

```csharp
_bannerView.SetAnimType(AnimType.SLIDE_LEFT); // Defaults to AnimType.NONE
```

#### &#x20;애니메이션 타입 종류

<table><thead><tr><th width="261">상수</th><th>설명</th><th data-hidden>설명</th><th data-hidden></th></tr></thead><tbody><tr><td><code>AnimType.NONE</code></td><td>No animation (default)</td><td></td><td></td></tr><tr><td><code>AnimType.FADE_IN</code></td><td>Fade-in animation</td><td></td><td></td></tr><tr><td><code>AnimType.SLIDE_LEFT</code></td><td>Slide animation to the left</td><td></td><td></td></tr><tr><td><code>AnimType.SLIDE_RIGHT</code></td><td>Slide animation to the right</td><td></td><td></td></tr><tr><td><code>AnimType.TOP_SLIDE</code></td><td>Slide animation from the top</td><td></td><td></td></tr><tr><td><code>AnimType.BOTTOM_SLIDE</code></td><td>Slide animation from the bottom</td><td></td><td></td></tr><tr><td><code>AnimType.CIRCLE</code></td><td>Banner rotation animation</td><td></td><td></td></tr></tbody></table>

### Set the network schedule timeout (optional)

Set the network schedule timeout for banner ads. (Default: 5 seconds) When loading ads, a timeout period is given for each network. If an ad is not received within that time, it will move to the next network.

```csharp
_bannerView.SetNetworkScheduleTimeout(5); // Default 5 seconds
```

### Set the banner ad request refresh interval (optional)

Set the refresh interval for banner ads. (Default: 60 seconds)\
The configurable range is between 15 and 300 seconds, and if set to **-1**, the ad will not refresh automatically.

```csharp
// The default is 60 seconds, with a setting range of 15 to 300 seconds.
_bannerView.SetRefreshTime(60);
```

### Fill the banner background color (optional)

You can fill the empty space of the banner view with a background color. (Default: **true**)

```csharp
_bannerView.SetAutoBgColor(true); // Defaults to true
```

### Set the banner mediation options (optional).

Supports configuration values for some mediation settings of the banner ads.

```csharp
/// <summary>
/// Extend the functionality of the mediation that appears in your ads.
/// </summary>
/// <seealso cref="MediationKey" />
/// <param name="ad"><see cref="OAMBannerView"/></param>
private void SetMediationExtras(OAMBannerView ad)
{
    var extras = new Dictionary<MediationKey, object>
    {
        // Set to true to show Cauly ads on the lock screen. (Defaults to false)
        { MediationKey.CAULY_ENABLE_LOCK, false },
        
        // Control ad serving intervals on Cauly's side. (Defaults to true)
        { MediationKey.CAULY_ENABLE_DYNAMIC_RELOAD_INTERVAL, true },
        
        // Controls how often ads appear in media. (Default 20 seconds)
        // (available after changing CAULY_DAYNAMIC_RELOAD_INTERVAL to False)
        { MediationKey.CAULY_RELOAD_INTERVAL, 20 },
        
        // Set thread priority.
        { MediationKey.CAULY_THREAD_PRIORITY, 5 },
        
        // Options to set age information for meso ads. (Defaults to -1)
        // Unknown = -1; Children(under 13) = 0; Teens and adults(over 13) = 1
        { MediationKey.MEZZO_AGE_LEVEL, -1 },
        
        // Option to allow the banner to run in the background. (true | false)
        { MediationKey.MEZZO_ENABLE_BACKGROUND_CHECK, false },
        
        // The app's Store URL
        { MediationKey.MEZZO_STORE_URL, "..." }
    };

    ad.SetMediationExtras(extras);
}
```

{% content-ref url="../sdk" %}
[sdk](https://one-admax-organization.gitbook.io/one-admax-sdk/oneadmax_dev_guide_en/sdk)
{% endcontent-ref %}

### Listen for events from the banner view (optional)

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

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

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

{% content-ref url="../oamerror" %}
[oamerror](https://one-admax-organization.gitbook.io/one-admax-sdk/oneadmax_dev_guide_en/oamerror)
{% endcontent-ref %}

### Create a banner view.

Complete all the settings and create th&#x65;**`OAMBannerView`**

```csharp
// These ad units are configured to always serve test ads.   
private readonly string _placementId = "String your placementID";  // 320 * 50

_bannerView.Create(_placementId);
```

### Banner Load

Call the **Load()** API at the desired time to request an ad from the server for displaying the banner ad.

```csharp
/// <summary>
/// Creates the banner view and loads a banner ad.
/// </summary>
public void Load()
{
    if (_bannerView == null)
    {
        Debug.LogWarning("The banner view is null.");
        return;
    }

    _bannerView.Load();
}
```

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

### Stop displaying the banner ad.

Call it when you no longer want to display the banner ad.

```csharp
/// <summary>
/// Destroys the ad.
/// When you are finished with a OAMBannerView, make sure to call
/// the Destroy() method before dropping your reference to it.
/// </summary>
public void Destroy()
{
    if (_bannerView != null)
    {
        Debug.Log("Destroying banner view.");
        _bannerView.Destroy();
        _bannerView = null;
    }
}
```

{% hint style="danger" %}
However, if the application is in a paused state, calling **Destroy()** may result in missing click reports. Therefore, it should be called when the **Activity** or **Fragment** containing the banner view is destroyed.
{% endhint %}

### Check whether the banner ad is loaded.

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

```csharp
_bannerView.IsLoaded();
```

### Extend the application lifecycle.

Implement to check the **onPause** and **onResume** status of the application where the banner ad is being displayed.

```csharp
public class BannerViewController : MonoBehaviour
{
    void OnApplicationPause(bool pauseStatus)
    {
        // You should call events for pause and resume in the application lifecycle.
        _bannerView?.OnPause(pauseStatus);
    }
}
```

{% hint style="warning" %}
If not handled, missing report data may occur when using third-party mediation.
{% endhint %}
