Skip to content

Commit

Permalink
#UnityAdapter Add adaptive banner support for Unity Android 3.4.8.0
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 336769842
  • Loading branch information
Mobile Ads Developer Relations authored and maddevrelgithubbot committed Oct 12, 2020
1 parent fb44b35 commit f89acf1
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
3 changes: 3 additions & 0 deletions ThirdPartyAdapters/unity/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## Unity Ads Android Mediation Adapter Changelog

#### Next Version
- Add adaptive banner support

#### Version 3.4.8.0
- Fixed a `NullPointerException` error that occurs when a banner ad is destroyed.
- Updated the minimum required Google Mobile Ads SDK version to 19.3.0.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,29 @@ public class UnityAdapter extends UnityMediationAdapter
*/
private MediationInterstitialListener mMediationInterstitialListener;

/** Placement ID used to determine what type of ad to load. */
/**
* Placement ID used to determine what type of ad to load.
*/
private String mPlacementId;

/** Placement ID for banner if requested. */
/**
* Placement ID for banner if requested.
*/
private String bannerPlacementId;

/** The view for the banner instance. */
/**
* The view for the banner instance.
*/
private BannerView mBannerView;

/** Callback object for Google's Banner Lifecycle. */
/**
* Callback object for Google's Banner Lifecycle.
*/
private MediationBannerListener mMediationBannerListener;

/** An Android {@link Activity} weak reference used to show ads. */
/**
* An Android {@link Activity} weak reference used to show ads.
*/
private WeakReference<Activity> mActivityWeakReference;

/**
Expand Down Expand Up @@ -245,7 +255,7 @@ public void onBannerLeftApplication(BannerView bannerAdView) {
/**
* Checks whether or not the provided Unity Ads IDs are valid.
*
* @param gameId Unity Ads Game ID to be verified.
* @param gameId Unity Ads Game ID to be verified.
* @param placementId Unity Ads Placement ID to be verified.
* @return {@code true} if all the IDs provided are valid.
*/
Expand Down Expand Up @@ -384,10 +394,12 @@ public void onDestroy() {
}

@Override
public void onPause() {}
public void onPause() {
}

@Override
public void onResume() {}
public void onResume() {
}
// endregion

// region MediationBannerAdapter implementation.
Expand All @@ -399,6 +411,22 @@ public void requestBannerAd(
AdSize adSize,
MediationAdRequest adRequest,
Bundle mediationExtras) {

// Convert requested size to unity Ad Size.
final UnityBannerSize unityBannerSize =
UnityAdsAdapterUtils.getUnityBannerSize(context, adSize);
if (unityBannerSize == null) {
String errorMessage =
createAdapterError(
ERROR_BANNER_SIZE_MISMATCH,
"There is no matching UnityAds ad size for Google ad size: " + adSize);
Log.w(TAG, errorMessage);
if (mMediationBannerListener != null) {
mMediationBannerListener.onAdFailedToLoad(UnityAdapter.this, ERROR_BANNER_SIZE_MISMATCH);
}
return;
}

Log.v(TAG, "Requesting Unity Ads Banner.");
mMediationBannerListener = listener;

Expand Down Expand Up @@ -441,14 +469,8 @@ public void requestBannerAd(
return;
}

float density = context.getResources().getDisplayMetrics().density;
int bannerWidth = Math.round(adSize.getWidthInPixels(context) / density);
int bannerHeight = Math.round(adSize.getHeightInPixels(context) / density);

UnityBannerSize size = new UnityBannerSize(bannerWidth, bannerHeight);

if (mBannerView == null) {
mBannerView = new BannerView((Activity) context, bannerPlacementId, size);
mBannerView = new BannerView((Activity) context, bannerPlacementId, unityBannerSize);
}

mBannerView.setListener(mUnityBannerListener);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package com.google.ads.mediation.unity;

import android.content.Context;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.google.ads.mediation.unity.UnityMediationAdapter.AdapterError;
import com.google.android.gms.ads.MediationUtils;
import com.unity3d.ads.UnityAds.UnityAdsError;
import com.unity3d.services.banners.BannerErrorInfo;
import com.unity3d.services.banners.UnityBannerSize;
import com.google.android.gms.ads.AdSize;
import java.util.ArrayList;

/** Utility class for the Unity adapter. */
public class UnityAdsAdapterUtils {
Expand Down Expand Up @@ -113,4 +119,20 @@ static int getMediationErrorCode(@NonNull UnityAdsError unityAdsError) {
}
return errorCode;
}

@Nullable
public static UnityBannerSize getUnityBannerSize(@NonNull Context context,
@NonNull AdSize adSize) {
ArrayList<AdSize> potentials = new ArrayList<>();
potentials.add(AdSize.BANNER);
potentials.add(AdSize.LEADERBOARD);

AdSize closestSize = MediationUtils.findClosestSize(context, adSize, potentials);
if (closestSize != null) {
return new UnityBannerSize(adSize.getWidth(), adSize.getHeight());
}

return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public class UnityMediationAdapter extends Adapter implements MediationRewardedA
ERROR_AD_NOT_READY,
ERROR_UNITY_ADS_NOT_SUPPORTED,
ERROR_AD_ALREADY_LOADING,
ERROR_FINISH
ERROR_FINISH,
ERROR_BANNER_SIZE_MISMATCH
})
@interface AdapterError {}

Expand Down Expand Up @@ -81,6 +82,9 @@ public class UnityMediationAdapter extends Adapter implements MediationRewardedA

/** UnityAds finished with a {@link FinishState#ERROR} state. */
static final int ERROR_FINISH = 109;

/** The requested ad size does not match a UnityAds supported banner size. */
static final int ERROR_BANNER_SIZE_MISMATCH = 110;
// endregion

/** Key to obtain Game ID, required for loading Unity Ads. */
Expand Down

0 comments on commit f89acf1

Please sign in to comment.