Unity SDK: Banners

Initialization

To display 320x50, 300x250 and 728x90 banners in your app, you must create an instance of the MyTargetView class. To create an instance of MyTargetView, specify your slotId. To specify the format, you must set one of AdSize values. The default format is 320x50.
private MyTargetView _myTargetView;
private void Awake()
{
    UInt32 slotId = 0;
#if UNITY_ANDROID
    slotId = ANDROID_SLOT_ID;
#elif UNITY_IOS
    slotId = IOS_SLOT_ID;
#endif
 
    // Enabling debug mode
    // MyTargetView.IsDebugMode = true;
 
    // Create an instance of MyTargetView, 320x50 format
    _myTargetView = new MyTargetView(slotId);
  
    // Create an instance of MyTargetView, 300x250 format
    // _myTargetView = new MyTargetView(slotId, AdSize.Size300x250);
}

Loading and displaying ads

To receive notifications (such as ad load succeeded, ad load failed, or ad clicked), you must set handlers to corresponding events. After ad has loaded successfully, you can start displaying ad.
private readonly Object _syncRoot = new Object();
private MyTargetView _myTargetView;
private void Awake()
{
    if (_myTargetView != null)
    {
        return;
    }  
    lock (_syncRoot)
    {
        if (_myTargetView != null)
        {
            return;
        }  
         
        // Create an instance of MyTargetView
        _myTargetView = new MyTargetView(YOUR_SLOT_ID);
     
        // Set event handlers
        _myTargetView.AdClicked += OnAdClicked;
        _myTargetView.AdLoadFailed += OnAdLoadFailed;
        _myTargetView.AdLoadCompleted += OnAdLoadCompleted;
        _myTargetView.AdShown += OnAdShown;
 
        // Start loading ad
        _myTargetView.Load();
    }
}
  
private void OnAdClicked(Object sender, EventArgs eventArgs) { }
 
private void OnAdShown(Object sender, EventArgs eventArgs) { }
 
private void OnAdLoadFailed(Object sender, ErrorEventArgs errorEventArgs) { }
  
private void OnAdLoadCompleted(Object sender, EventArgs eventArgs)
{
    // The ad is successfully loaded
  
    // Set position on the screen
    _myTargetView.X = 0;
    _myTargetView.Y = 0;
         
    // Start displaying ad
    _myTargetView.Start();
}
  
private void OnDestroy()
{
    if (_myTargetView == null)
    {
        return;
    }
    lock (_syncRoot)
    {
        if (_myTargetView == null)
        {
            return;
        }
        _myTargetView.Dispose();
        _myTargetView = null;
    }
}

Rotation

Ads are rotated every 60 seconds. You can disable automatic ad rotation by specifying the optional isRefreshAd parameter when creating the instance. Rotation is only available for 320x50 and 728x90 formats.

// Disable automatic ad rotation
_myTargetView = new MyTargetView(YOUR_SLOT_ID, AdSize.Size320x50, false)
Example of banner 320x50
Was this article helpful?
Да
Нет