Banner & MREC Ads
The following sections show you how to load and then show and hide a banner or MREC ad.
Loading a Banner or MREC
To load a banner or MREC, use code like the following with your ad unit ID and desired ad view position:
#if UNITY_IOSstring bannerAdUnitId = "«iOS-ad-unit-ID»"; // Retrieve the ID from your account#else // UNITY_ANDROIDstring bannerAdUnitId = "«Android-ad-unit-ID»"; // Retrieve the ID from your account#endif
public void InitializeBannerAds(){ // Banners are automatically sized to 320×50 on phones and 728×90 on tablets // You may call the utility method MaxSdkUtils.isTablet() to help with view sizing adjustments MaxSdk.CreateBanner(bannerAdUnitId, MaxSdkBase.BannerPosition.BottomCenter);
// Set background color for banners to be fully functional MaxSdk.SetBannerBackgroundColor(bannerAdUnitId, «banner-background-color»);
MaxSdkCallbacks.Banner.OnAdLoadedEvent += OnBannerAdLoadedEvent; MaxSdkCallbacks.Banner.OnAdLoadFailedEvent += OnBannerAdLoadFailedEvent; MaxSdkCallbacks.Banner.OnAdClickedEvent += OnBannerAdClickedEvent; MaxSdkCallbacks.Banner.OnAdRevenuePaidEvent += OnBannerAdRevenuePaidEvent; MaxSdkCallbacks.Banner.OnAdExpandedEvent += OnBannerAdExpandedEvent; MaxSdkCallbacks.Banner.OnAdCollapsedEvent += OnBannerAdCollapsedEvent;}
private void OnBannerAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}
private void OnBannerAdLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo) {}
private void OnBannerAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}
private void OnBannerAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}
private void OnBannerAdExpandedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}
private void OnBannerAdCollapsedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}
Set your banner background color to a #-prefixed hexadecimal RGB string, for example '#000000'
(black) or '#fff200'
(yellow).
#if UNITY_IOSstring mrecAdUnitId = "«iOS-ad-unit-ID»"; // Retrieve the ID from your account#else // UNITY_ANDROIDstring mrecAdUnitId = "«Android-ad-unit-ID»"; // Retrieve the ID from your account#endif
public void InitializeMRecAds(){ // MRECs are sized to 300x250 on phones and tablets MaxSdk.CreateMRec(mrecAdUnitId, MaxSdkBase.AdViewPosition.Centered);
MaxSdkCallbacks.MRec.OnAdLoadedEvent += OnMRecAdLoadedEvent; MaxSdkCallbacks.MRec.OnAdLoadFailedEvent += OnMRecAdLoadFailedEvent; MaxSdkCallbacks.MRec.OnAdClickedEvent += OnMRecAdClickedEvent; MaxSdkCallbacks.MRec.OnAdRevenuePaidEvent += OnMRecAdRevenuePaidEvent; MaxSdkCallbacks.MRec.OnAdExpandedEvent += OnMRecAdExpandedEvent; MaxSdkCallbacks.MRec.OnAdCollapsedEvent += OnMRecAdCollapsedEvent;}
public void OnMRecAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}
public void OnMRecAdLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo error) {}
public void OnMRecAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}
public void OnMRecAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}
public void OnMRecAdExpandedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}
public void OnMRecAdCollapsedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}
The complete list of position options are:
TopLeft
TopCenter
TopRight
Centered
CenterLeft
CenterRight
BottomLeft
BottomCenter
BottomRight
You can also position an ad at a specific (x, y) coordinate on the screen by calling MaxSdk.CreateBanner(«ad-unit-ID», «x», «y»);
or MaxSdk.CreateMRec(«ad-unit-ID», «x», «y»);
.
This sets the position of the top-left corner of the ad.
The coordinate system represents the safe area bounds of the screen.
Make sure to account for the width and height of the ad when you set these coordinates.
The position (0, 0) is equivalent to TopLeft
; the bottom-right corner of the safe area is (safeAreaWidth, safeAreaHeight).
Note that Unity might have a different screen size or safe area size than Android or iOS.
To convert between Unity’s screen size and the sizes used in Android or iOS, use code like the following:
var density = MaxSdkUtils.GetScreenDensity();var dp = «pixels» / density;
Showing a Banner or MREC
To show a banner or MREC, make the following call:
MaxSdk.ShowBanner(«ad-unit-ID»);
MaxSdk.ShowMRec(«ad-unit-ID»);
Hiding a Banner or MREC
To hide a banner or MREC, make the following call:
MaxSdk.HideBanner(«ad-unit-ID»);
MaxSdk.HideMRec(«ad-unit-ID»);
Destroying Banners or MRECs
You may no longer need an ad instance (for example, if the user purchased ad removal).
If so, call the DestroyBanner()
or DestroyMRec()
method to free resources.
Do not call DestroyBanner()
or DestroyMRec()
if you use multiple ad instances with the same Ad Unit ID.
MaxSdk.DestroyBanner(«ad-unit-ID»);
MaxSdk.DestroyMRec(«ad-unit-ID»);
Getting Banner Position
To get the banner’s position and size, call GetBannerLayout()
.
This uses the same Unity coordinate system as explained in Loading a Banner or MREC.
Rect bannerLayout = MaxSdk.GetBannerLayout(«ad-unit-ID»);
Setting Banner Width
To manually set the banner’s width, call SetBannerWidth()
.
Set the width to a size larger than the minimum value (320 on phones, 728 on tablets).
Banners under this width may not be considered viewable by the advertiser, which will affect your revenue:
MaxSdk.SetBannerWidth(«ad-unit-ID», «width»);
If you set the banner width below the minimum, you will see an error message in your logs. For example:
[AppLovinSdk] [MAUnityAdManager] The provided width: 300.000000 is smaller than the minimum required width: 320.000000 for ad format: [MAAdFormat: BANNER]. Please set the width higher than the minimum required.
Adaptive Banners
Adaptive banners are responsive banners with heights that derive from the device type and width of the banner.
Banners from ad networks that support adaptive banners are adaptive by default.
If you want to disable adaptive banners, set the banner extra parameter adaptive_banner
to false
when you create the banner, as in the following example:
#if UNITY_IOSstring bannerAdUnitId = "«iOS-ad-unit-ID»"; // Retrieve the ID from your account#else // UNITY_ANDROIDstring bannerAdUnitId = "«android-ad-unit-ID»"; // Retrieve the ID from your account#endif
public void InitializeBannerAds(){ MaxSdk.CreateBanner(bannerAdUnitId, MaxSdkBase.BannerPosition.BottomCenter); MaxSdk.SetBannerExtraParameter(bannerAdUnitId, "adaptive_banner", "false");
// Set background or background color for banners to be fully functional MaxSdk.SetBannerBackgroundColor(bannerAdUnitId, «background-color»);}
Call MaxSdkUtils.GetAdaptiveBannerHeight()
to get the banner height, and then adjust your content accordingly.
Stopping and Starting Auto-Refresh
You may want to stop auto-refresh for an ad, for instance if you want to manually refresh banner ads. To stop auto-refresh for a banner or MREC ad, use the following code:
MaxSdk.StopBannerAutoRefresh(«ad-unit-ID»);
MaxSdk.StopMRecAutoRefresh(«ad-unit-ID»);
Start auto-refresh for a banner or MREC ad with the following code:
MaxSdk.StartBannerAutoRefresh(«ad-unit-ID»);
MaxSdk.StartMRecAutoRefresh(«ad-unit-ID»);
Manually refresh the contents with the following code.
You must stop auto-refresh before you call LoadBanner()
or LoadMRec()
.
MaxSdk.LoadBanner(«ad-unit-ID»);
MaxSdk.LoadMRec(«ad-unit-ID»);