Unity SDK Usage
Banner&MREC Ads
Once the initialization of the SDK is complete, you can get and load banner and mrec ads from the Empower AppLovin SDK.
Loading a Banner or MREC
Banners
#if UNITY_IOSstring bannerAdUnitId = "«iOS-ad-unit-ID»"; // «iOS-ad-unit-ID» will be provided by us.#else // UNITY_ANDROIDstring bannerAdUnitId = "«Android-ad-unit-ID»"; // «Android-ad-unit-ID» will be provided by us.#endifpublic 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 adjustmentsMaxSdk.CreateBanner(bannerAdUnitId, MaxSdkBase.BannerPosition.BottomCenter);// Set background or background color for banners to be fully functionalMaxSdk.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) {}
MRECs
#if UNITY_IOSstring mrecAdUnitId = "«iOS-ad-unit-ID»"; «iOS-ad-unit-ID» will be provided by us.#else // UNITY_ANDROIDstring mrecAdUnitId = "«Android-ad-unit-ID»"; «Android-ad-unit-ID» will be provided by us.#endifpublic void InitializeMRecAds(){// MRECs are sized to 300x250 on phones and tabletsMaxSdk.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
var density = MaxSdkUtils.GetScreenDensity();var dp = «pixels» / density;
Note: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:
Displaying a Banner
MaxSdk.ShowBanner(«ad-unit-ID»);
Hiding a Banner
MaxSdk.HideBanner(«ad-unit-ID»);
Displaying a MREC
MaxSdk.ShowMRec(«ad-unit-ID»);
Hiding a MREC
MaxSdk.HideMRec(«ad-unit-ID»);
Destroying Banners or MRECs
MaxSdk.DestroyBanner(«ad-unit-ID»);
MaxSdk.DestroyMRec(«ad-unit-ID»);
Getting Banner Position
Rect bannerLayout = MaxSdk.GetBannerLayout(«ad-unit-ID»);
Setting Banner Width
MaxSdk.SetBannerWidth(«ad-unit-ID», «width»);
App Open Ads
public class HomeScreen : MonoBehaviour{void Start(){MaxSdkCallbacks.OnSdkInitializedEvent += sdkConfiguration =>{MaxSdkCallbacks.AppOpen.OnAdHiddenEvent += OnAppOpenDismissedEvent;AppOpenManager.Instance.ShowAdIfReady();};MaxSdk.SetSdkKey("«SDK-key»");MaxSdk.InitializeSdk();}public void OnAppOpenDismissedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo){MaxSdk.LoadAppOpenAd(AppOpenAdUnitId);}private void OnApplicationPause(bool pauseStatus){if (!pauseStatus){AppOpenManager.Instance.ShowAdIfReady();}}}public class AppOpenManager{#if UNITY_IOSprivate const string AppOpenAdUnitId = "«iOS-ad-unit-ID»";#else // UNITY_ANDROIDprivate const string AppOpenAdUnitId = "«Android-ad-unit-ID»";#endifpublic void ShowAdIfReady(){if (MaxSdk.IsAppOpenAdReady(AppOpenAdUnitId)){MaxSdk.ShowAppOpenAd(AppOpenAdUnitId);}else{MaxSdk.LoadAppOpenAd(AppOpenAdUnitId);}}}
Interstitial Ads
Load an Interstitial Ad
#if UNITY_IOSstring adUnitId = "«iOS-ad-unit-ID»";#else // UNITY_ANDROIDstring adUnitId = "«Android-ad-unit-ID»";#endifpublic void InitializeInterstitialAds(){// Attach callbackMaxSdkCallbacks.Interstitial.OnAdLoadedEvent += OnInterstitialLoadedEvent;MaxSdkCallbacks.Interstitial.OnAdLoadFailedEvent += OnInterstitialLoadFailedEvent;MaxSdkCallbacks.Interstitial.OnAdDisplayedEvent += OnInterstitialDisplayedEvent;MaxSdkCallbacks.Interstitial.OnAdClickedEvent += OnInterstitialClickedEvent;MaxSdkCallbacks.Interstitial.OnAdHiddenEvent += OnInterstitialHiddenEvent;MaxSdkCallbacks.Interstitial.OnAdDisplayFailedEvent += OnInterstitialAdFailedToDisplayEvent;// Load the first interstitialLoadInterstitial();}private void LoadInterstitial(){MaxSdk.LoadInterstitial(adUnitId);}private void OnInterstitialLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo){// Interstitial ad is ready for you to show. MaxSdk.IsInterstitialReady(adUnitId) now returns 'true'}private void OnInterstitialLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo){// Interstitial ad failed to load}private void OnInterstitialDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}private void OnInterstitialAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo){// Interstitial ad failed to display. AppLovin recommends that you load the next ad.LoadInterstitial();}private void OnInterstitialClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}private void OnInterstitialHiddenEvent(string adUnitId, MaxSdkBase.AdInfo adInfo){// Interstitial ad is hidden. Pre-load the next ad.LoadInterstitial();}
Showing an Interstitial Ad
if ( MaxSdk.IsInterstitialReady(adUnitId) ){MaxSdk.ShowInterstitial(adUnitId);}
Rewarded Ads
Load a Rewarded Ad
#if UNITY_IOSstring adUnitId = "«iOS-ad-unit-ID»";#else // UNITY_ANDROIDstring adUnitId = "«Android-ad-unit-ID»";#endifpublic void InitializeRewardedAds(){// Attach callbackMaxSdkCallbacks.Rewarded.OnAdLoadedEvent += OnRewardedAdLoadedEvent;MaxSdkCallbacks.Rewarded.OnAdLoadFailedEvent += OnRewardedAdLoadFailedEvent;MaxSdkCallbacks.Rewarded.OnAdDisplayedEvent += OnRewardedAdDisplayedEvent;MaxSdkCallbacks.Rewarded.OnAdClickedEvent += OnRewardedAdClickedEvent;MaxSdkCallbacks.Rewarded.OnAdRevenuePaidEvent += OnRewardedAdRevenuePaidEvent;MaxSdkCallbacks.Rewarded.OnAdHiddenEvent += OnRewardedAdHiddenEvent;MaxSdkCallbacks.Rewarded.OnAdDisplayFailedEvent += OnRewardedAdFailedToDisplayEvent;MaxSdkCallbacks.Rewarded.OnAdReceivedRewardEvent += OnRewardedAdReceivedRewardEvent;// Load the first rewarded adLoadRewardedAd();}private void LoadRewardedAd(){MaxSdk.LoadRewardedAd(adUnitId);}private void OnRewardedAdLoadedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo){// Rewarded ad is ready for you to show. MaxSdk.IsRewardedAdReady(adUnitId) now returns 'true'.}private void OnRewardedAdLoadFailedEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo){// Rewarded ad failed to load}private void OnRewardedAdDisplayedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}private void OnRewardedAdFailedToDisplayEvent(string adUnitId, MaxSdkBase.ErrorInfo errorInfo, MaxSdkBase.AdInfo adInfo){// Rewarded ad failed to display. AppLovin recommends that you load the next ad.LoadRewardedAd();}private void OnRewardedAdClickedEvent(string adUnitId, MaxSdkBase.AdInfo adInfo) {}private void OnRewardedAdHiddenEvent(string adUnitId, MaxSdkBase.AdInfo adInfo){// Rewarded ad is hidden. Pre-load the next adLoadRewardedAd();}private void OnRewardedAdReceivedRewardEvent(string adUnitId, MaxSdk.Reward reward, MaxSdkBase.AdInfo adInfo){// The rewarded ad displayed and the user should receive the reward.}private void OnRewardedAdRevenuePaidEvent(string adUnitId, MaxSdkBase.AdInfo adInfo){// Ad revenue paid. Use this callback to track user revenue.}
Showing a Rewarded Ad
if (MaxSdk.IsRewardedAdReady(adUnitId)){MaxSdk.ShowRewardedAd(adUnitId);}