Skip to content

Commit 03cf24b

Browse files
committed
Version 3.0.4 of the Google Mobile Ads Plugin
1 parent f1cad43 commit 03cf24b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1670
-222
lines changed

ChangeLog.txt

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
Google Mobile Ads Unity Plugin Change Log
22

3+
*************
4+
Version 3.0.4
5+
*************
6+
- Fix Podfile compatibility with CocoaPods 1.0.0.
7+
- Add support for DFP custom native ad formats.
8+
9+
Built and tested with:
10+
- Google Play services 9.0.0
11+
- Google Mobile Ads iOS SDK 7.8.1
12+
- Unity Jar Resolver 1.2
13+
314
*************
415
Version 3.0.3
516
*************
617
- Restrict simultaneous rewarded video requests on Android.
718

819
Built and tested with:
9-
- Google Play Services 8.4.0
20+
- Google Play services 8.4.0
1021
- Google Mobile Ads iOS SDK 7.7.0
1122

1223
*************
@@ -15,7 +26,7 @@ Version 3.0.2
1526
- Fix compatibility issues with Google Mobile Ads iOS SDK 7.7.0
1627

1728
Built and tested with:
18-
- Google Play Services 8.4.0
29+
- Google Play services 8.4.0
1930
- Google Mobile Ads iOS SDK 7.7.0
2031

2132
*************
@@ -25,7 +36,7 @@ Version 3.0.1
2536
- Add request agent to all ad requests from plugin
2637

2738
Built and tested with:
28-
- Google Play Services 8.4.0
39+
- Google Play services 8.4.0
2940
- Google Mobile Ads iOS SDK 7.6.0
3041

3142
*************
@@ -37,7 +48,7 @@ Version 3.0.0
3748
- Ad events for banners and interstitials refactored with new names
3849

3950
Built and tested with:
40-
- Google Play Services 8.4.0
51+
- Google Play services 8.4.0
4152
- Google Mobile Ads iOS SDK 7.6.0
4253

4354
*************

GoogleMobileAds.unitypackage

-79.9 KB
Binary file not shown.

samples/HelloWorld/Assets/GoogleMobileAdsDemoScript.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private void RequestBanner()
147147
string adUnitId = "unused";
148148
#elif UNITY_ANDROID
149149
string adUnitId = "INSERT_ANDROID_BANNER_AD_UNIT_ID_HERE";
150-
#elif UNITY_IPHONE
150+
#elif (UNITY_5 && UNITY_IOS) || UNITY_IPHONE
151151
string adUnitId = "INSERT_IOS_BANNER_AD_UNIT_ID_HERE";
152152
#else
153153
string adUnitId = "unexpected_platform";
@@ -171,7 +171,7 @@ private void RequestInterstitial()
171171
string adUnitId = "unused";
172172
#elif UNITY_ANDROID
173173
string adUnitId = "INSERT_ANDROID_INTERSTITIAL_AD_UNIT_ID_HERE";
174-
#elif UNITY_IPHONE
174+
#elif (UNITY_5 && UNITY_IOS) || UNITY_IPHONE
175175
string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
176176
#else
177177
string adUnitId = "unexpected_platform";
@@ -209,7 +209,7 @@ private void RequestRewardBasedVideo()
209209
string adUnitId = "unused";
210210
#elif UNITY_ANDROID
211211
string adUnitId = "INSERT_ANDROID_REWARD_BASED_VIDEO_AD_UNIT_ID_HERE";
212-
#elif UNITY_IPHONE
212+
#elif (UNITY_5 && UNITY_IOS) || UNITY_IPHONE
213213
string adUnitId = "INSERT_IOS_REWARD_BASED_VIDEO_AD_UNIT_ID_HERE";
214214
#else
215215
string adUnitId = "unexpected_platform";
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright (C) 2016 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.unity.ads;
17+
18+
import android.app.Activity;
19+
import android.graphics.Bitmap;
20+
import android.graphics.drawable.BitmapDrawable;
21+
import android.graphics.drawable.Drawable;
22+
23+
import com.google.android.gms.ads.formats.NativeCustomTemplateAd;
24+
25+
import java.io.ByteArrayOutputStream;
26+
import java.util.List;
27+
28+
/**
29+
* Native AdLoader implementation for the Google Mobile Ads Unity plugin.
30+
*/
31+
public class CustomNativeAd {
32+
33+
/**
34+
* The {@code Activity} on which the custom native template ad will display.
35+
*/
36+
private Activity activity;
37+
38+
/**
39+
* The {@link NativeCustomTemplateAd}.
40+
*/
41+
private NativeCustomTemplateAd nativeAd;
42+
43+
public CustomNativeAd(Activity activity, NativeCustomTemplateAd ad) {
44+
this.activity = activity;
45+
this.nativeAd = ad;
46+
}
47+
48+
/**
49+
* Returns a list of all available assets.
50+
*/
51+
public String[] getAvailableAssetNames() {
52+
List<String> assetNames = nativeAd.getAvailableAssetNames();
53+
return assetNames.toArray(new String[assetNames.size()]);
54+
}
55+
56+
/**
57+
* Returns the ID of the custom template used to request this ad.
58+
*/
59+
public String getTemplateId() {
60+
return nativeAd.getCustomTemplateId();
61+
}
62+
63+
64+
/**
65+
* Called when the user has clicked on the ad.
66+
*
67+
* @param assetName The name of the asset that was clicked.
68+
*/
69+
public void performClick(final String assetName) {
70+
activity.runOnUiThread(new Runnable() {
71+
@Override
72+
public void run() {
73+
nativeAd.performClick(assetName);
74+
}
75+
});
76+
}
77+
78+
/**
79+
* Record an impression for the custom template ad.
80+
*/
81+
public void recordImpression() {
82+
activity.runOnUiThread(new Runnable() {
83+
@Override
84+
public void run() {
85+
nativeAd.recordImpression();
86+
}
87+
});
88+
}
89+
90+
/**
91+
* Returns an image asset.
92+
*
93+
* @param key The name of the asset to be retrieved.
94+
*/
95+
public byte[] getImage(String key) {
96+
Drawable image = nativeAd.getImage(key).getDrawable();
97+
Bitmap bitmap = ((BitmapDrawable) image).getBitmap();
98+
ByteArrayOutputStream stream = new ByteArrayOutputStream();
99+
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
100+
return stream.toByteArray();
101+
}
102+
103+
/**
104+
* Returns a string asset of numbers, URLs, or other types other than an image asset.
105+
*
106+
* @param key The name of the asset to be retrieved.
107+
*/
108+
public String getText(String key) {
109+
return nativeAd.getText(key).toString();
110+
}
111+
}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (C) 2016 Google, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.google.unity.ads;
17+
18+
import android.app.Activity;
19+
20+
import com.google.android.gms.ads.AdListener;
21+
import com.google.android.gms.ads.AdLoader;
22+
import com.google.android.gms.ads.AdRequest;
23+
import com.google.android.gms.ads.formats.NativeCustomTemplateAd;
24+
25+
/**
26+
* Native AdLoader implementation for the Google Mobile Ads Unity plugin.
27+
*/
28+
public class NativeAdLoader {
29+
30+
/**
31+
* The {@code Activity} on which the native ads will display.
32+
*/
33+
private Activity mActivity;
34+
35+
/**
36+
* The {@code AdLoader} used to load native ads.
37+
*/
38+
private AdLoader mAdLoader;
39+
40+
/**
41+
* The {@code AdLoader.Builder} used to construct a AdLoader.
42+
*/
43+
private AdLoader.Builder mAdLoaderBuilder;
44+
45+
/**
46+
* A listener implemented in Unity via {@code AndroidJavaProxy} to receive ad events.
47+
*/
48+
private UnityCustomNativeAdListener mListener;
49+
50+
51+
public NativeAdLoader(final Activity activity, final String adUnitId,
52+
UnityCustomNativeAdListener listener) {
53+
this.mActivity = activity;
54+
this.mListener = listener;
55+
56+
activity.runOnUiThread(new Runnable() {
57+
@Override
58+
public void run() {
59+
mAdLoaderBuilder = new AdLoader.Builder(activity, adUnitId);
60+
}
61+
});
62+
}
63+
64+
/**
65+
* Creates a {@link AdLoader.Builder}.
66+
*/
67+
public void create() {
68+
mActivity.runOnUiThread(new Runnable() {
69+
@Override
70+
public void run() {
71+
mAdLoader = mAdLoaderBuilder.build();
72+
}
73+
});
74+
}
75+
76+
/**
77+
* Configure ad loader to request custom native template ad.
78+
*
79+
* @param templateID Custom template Id defined in the DFP front end.
80+
* @param useClickListener Set whether to use optional listener that handles custom clicks on
81+
* custom template ads.
82+
*/
83+
public void configureCustomNativeTemplateAd(final String templateID, final boolean
84+
useClickListener) {
85+
mActivity.runOnUiThread(new Runnable() {
86+
@Override
87+
public void run() {
88+
NativeCustomTemplateAd.OnCustomClickListener clickListener = null;
89+
if (useClickListener) {
90+
clickListener = new NativeCustomTemplateAd.OnCustomClickListener() {
91+
@Override
92+
public void onCustomClick(NativeCustomTemplateAd ad, String assetName) {
93+
mListener.onCustomClick(new CustomNativeAd(mActivity, ad), assetName);
94+
}
95+
};
96+
}
97+
98+
mAdLoaderBuilder = mAdLoaderBuilder
99+
.forCustomTemplateAd(templateID,
100+
new NativeCustomTemplateAd.OnCustomTemplateAdLoadedListener() {
101+
@Override
102+
public void onCustomTemplateAdLoaded(
103+
NativeCustomTemplateAd ad) {
104+
mListener.onCustomTemplateAdLoaded(new CustomNativeAd
105+
(mActivity, ad));
106+
}
107+
}, clickListener)
108+
.withAdListener(new AdListener() {
109+
@Override
110+
public void onAdFailedToLoad(int errorCode) {
111+
mListener.onAdFailedToLoad(PluginUtils.getErrorReason(errorCode));
112+
}
113+
});
114+
}
115+
});
116+
}
117+
118+
/**
119+
* Send a request for an ad.
120+
*/
121+
public void loadAd(final AdRequest request) {
122+
mActivity.runOnUiThread(new Runnable() {
123+
@Override
124+
public void run() {
125+
mAdLoader.loadAd(request);
126+
}
127+
});
128+
}
129+
}

source/android-library/app/src/main/java/com/google/unity/ads/RewardBasedVideo.java

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@
1616
package com.google.unity.ads;
1717

1818
import android.app.Activity;
19-
import android.os.Bundle;
2019
import android.util.Log;
2120

22-
import com.google.ads.mediation.admob.AdMobAdapter;
2321
import com.google.android.gms.ads.AdRequest;
2422
import com.google.android.gms.ads.MobileAds;
2523
import com.google.android.gms.ads.reward.RewardItem;
@@ -51,18 +49,12 @@ public class RewardBasedVideo {
5149
*/
5250
private boolean isLoaded;
5351

54-
/**
55-
* Whether or not there is an ad request in progress.
56-
*/
57-
private boolean isLoading;
58-
5952
private final Object mLock = new Object();
6053

6154
public RewardBasedVideo(Activity activity, UnityRewardBasedVideoAdListener adListener) {
6255
this.activity = activity;
6356
this.adListener = adListener;
6457
this.isLoaded = false;
65-
this.isLoading = false;
6658
}
6759

6860
/**
@@ -76,19 +68,13 @@ public void run() {
7668
rewardBasedVideo.setRewardedVideoAdListener(new RewardedVideoAdListener() {
7769
@Override
7870
public void onRewardedVideoAdLoaded() {
79-
synchronized (mLock) {
80-
isLoaded = true;
81-
isLoading = false;
82-
adListener.onAdLoaded();
83-
}
71+
isLoaded = true;
72+
adListener.onAdLoaded();
8473
}
8574

8675
@Override
8776
public void onRewardedVideoAdFailedToLoad(int errorCode) {
88-
synchronized (mLock) {
89-
isLoading = false;
90-
adListener.onAdFailedToLoad(PluginUtils.getErrorReason(errorCode));
91-
}
77+
adListener.onAdFailedToLoad(PluginUtils.getErrorReason(errorCode));
9278
}
9379

9480
@Override
@@ -130,21 +116,7 @@ public void loadAd(final AdRequest request, final String adUnitId) {
130116
activity.runOnUiThread(new Runnable() {
131117
@Override
132118
public void run() {
133-
Bundle extras = request.getNetworkExtrasBundle(AdMobAdapter.class);
134-
// Bundle should be present as it is set by Unity plugin.
135-
if (extras != null) {
136-
extras.putBoolean("_noRefresh", true);
137-
}
138-
synchronized (mLock) {
139-
if (!isLoading) {
140-
isLoading = true;
141-
rewardBasedVideo.loadAd(adUnitId, request);
142-
} else {
143-
Log.w(PluginUtils.LOGTAG, "Cannot make a new rewarded video ad request "
144-
+ "until previous request has completed.");
145-
}
146-
}
147-
119+
rewardBasedVideo.loadAd(adUnitId, request);
148120
}
149121
});
150122
}

0 commit comments

Comments
 (0)