Skip to content

Commit

Permalink
Version 3.0.5 of the Google Mobile Ads Unity Plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Ram Parameswaran committed Jul 7, 2016
1 parent 03cf24b commit 27ea48f
Show file tree
Hide file tree
Showing 19 changed files with 289 additions and 168 deletions.
10 changes: 10 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
Google Mobile Ads Unity Plugin Change Log

*************
Version 3.0.5
*************
- Remove use of JSONUtility.

Built and tested with:
- Google Play services 9.2.0
- Google Mobile Ads iOS SDK 7.8.1
- Unity Jar Resolver 1.2

*************
Version 3.0.4
*************
Expand Down
4 changes: 2 additions & 2 deletions source/android-library/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ android {

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.google.android.gms:play-services:8.3.0'
compile 'com.android.support:appcompat-v7:23.4.0'
compile 'com.google.android.gms:play-services:9.2.0'
}

task clearJar(type: Delete) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import com.google.android.gms.ads.formats.NativeAd;
import com.google.android.gms.ads.formats.NativeCustomTemplateAd;

import java.io.ByteArrayOutputStream;
Expand Down Expand Up @@ -93,8 +94,13 @@ public void run() {
* @param key The name of the asset to be retrieved.
*/
public byte[] getImage(String key) {
Drawable image = nativeAd.getImage(key).getDrawable();
Bitmap bitmap = ((BitmapDrawable) image).getBitmap();
NativeAd.Image imageAsset = nativeAd.getImage(key);
if (imageAsset == null) {
return new byte[0];
}

Drawable imageDrawable = imageAsset.getDrawable();
Bitmap bitmap = ((BitmapDrawable) imageDrawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
Expand All @@ -106,6 +112,11 @@ public byte[] getImage(String key) {
* @param key The name of the asset to be retrieved.
*/
public String getText(String key) {
CharSequence assetText = nativeAd.getText(key);
if (assetText == null) {
return "";
}

return nativeAd.getText(key).toString();
}
}
100 changes: 58 additions & 42 deletions source/plugin/Assets/GoogleMobileAds/Api/AdLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@

using System;
using System.Collections.Generic;
using UnityEngine;

using GoogleMobileAds.Common;
using UnityEngine;

namespace GoogleMobileAds.Api
{
Expand All @@ -26,43 +27,84 @@ public enum NativeAdType

public class AdLoader
{
private IAdLoaderClient adLoaderClient;

private AdLoader(Builder builder)
{
this.AdUnitId = string.Copy(builder.AdUnitId);
this.CustomNativeTemplateClickHandlers =
new Dictionary<string, Action<CustomNativeTemplateAd, string>>(
builder.CustomNativeTemplateClickHandlers);
this.TemplateIds = new HashSet<string>(builder.TemplateIds);
this.AdTypes = new HashSet<NativeAdType>(builder.AdTypes);
this.adLoaderClient = GoogleMobileAdsClientFactory.BuildAdLoaderClient(this);

this.adLoaderClient.OnCustomNativeTemplateAdLoaded +=
delegate(object sender, CustomNativeEventArgs args)
{
this.OnCustomNativeTemplateAdLoaded(this, args);
};
this.adLoaderClient.OnAdFailedToLoad += delegate(
object sender, AdFailedToLoadEventArgs args)
{
this.OnAdFailedToLoad(this, args);
};
}

public event EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;
public event EventHandler<CustomNativeEventArgs> onCustomNativeTemplateAdLoaded;

private IAdLoaderClient adLoaderClient;
public event EventHandler<CustomNativeEventArgs> OnCustomNativeTemplateAdLoaded;

public Dictionary<string, Action<CustomNativeTemplateAd, string>>
CustomNativeTemplateClickHandlers
{
get; private set;
}

public string AdUnitId { get; private set; }

public HashSet<NativeAdType> AdTypes { get; private set; }

public HashSet<string> TemplateIds { get; private set; }
public Dictionary<string, Action<CustomNativeTemplateAd, string>>
CustomNativeTemplateClickHandlers { get; private set; }

public class Builder
public void LoadAd(AdRequest request)
{
internal string AdUnitId { get; private set; }
internal HashSet<NativeAdType> AdTypes { get; private set; }
internal HashSet<string> TemplateIds { get; private set; }
internal Dictionary<string, Action<CustomNativeTemplateAd, string>>
CustomNativeTemplateClickHandlers { get; private set; }
this.adLoaderClient.LoadAd(request);
}

public class Builder
{
public Builder(string adUnitId)
{
this.AdUnitId = adUnitId;
this.AdTypes = new HashSet<NativeAdType>();
this.TemplateIds = new HashSet<string>();
this.CustomNativeTemplateClickHandlers = new Dictionary<string,
Action<CustomNativeTemplateAd, string>>();
this.CustomNativeTemplateClickHandlers =
new Dictionary<string, Action<CustomNativeTemplateAd, string>>();
}

internal string AdUnitId { get; private set; }

internal HashSet<NativeAdType> AdTypes { get; private set; }

internal HashSet<string> TemplateIds { get; private set; }

internal Dictionary<string, Action<CustomNativeTemplateAd, string>>
CustomNativeTemplateClickHandlers
{
get; private set;
}

public Builder forCustomNativeAd(string templateId)
public Builder ForCustomNativeAd(string templateId)
{
this.TemplateIds.Add(templateId);
this.AdTypes.Add(NativeAdType.CustomTemplate);
return this;
}

public Builder forCustomNativeAd(string templateId,
Action<CustomNativeTemplateAd, string> callback)
public Builder ForCustomNativeAd(
string templateId,
Action<CustomNativeTemplateAd, string> callback)
{
this.TemplateIds.Add(templateId);
this.CustomNativeTemplateClickHandlers[templateId] = callback;
Expand All @@ -75,31 +117,5 @@ public AdLoader Build()
return new AdLoader(this);
}
}

private AdLoader(Builder builder)
{
AdUnitId = String.Copy(builder.AdUnitId);
CustomNativeTemplateClickHandlers = new Dictionary<string,
Action<CustomNativeTemplateAd, string>>(builder.CustomNativeTemplateClickHandlers);
TemplateIds = new HashSet<string>(builder.TemplateIds);
AdTypes = new HashSet<NativeAdType>(builder.AdTypes);

adLoaderClient = GoogleMobileAdsClientFactory.BuildAdLoaderClient(this);

adLoaderClient.onCustomNativeTemplateAdLoaded +=
delegate(object sender, CustomNativeEventArgs args) {
onCustomNativeTemplateAdLoaded(this, args);
};

adLoaderClient.OnAdFailedToLoad += delegate(object sender, AdFailedToLoadEventArgs args)
{
OnAdFailedToLoad(this, args);
};
}

public void LoadAd(AdRequest request)
{
adLoaderClient.LoadAd(request);
}
}
}
2 changes: 1 addition & 1 deletion source/plugin/Assets/GoogleMobileAds/Api/AdRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace GoogleMobileAds.Api
{
public class AdRequest
{
public const string Version = "3.0.4";
public const string Version = "3.0.5";
public const string TestDeviceSimulator = "SIMULATOR";

public class Builder
Expand Down
26 changes: 19 additions & 7 deletions source/plugin/Assets/GoogleMobileAds/Api/CustomNativeTemplateAd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

using System;
using System.Collections.Generic;
using UnityEngine;

using GoogleMobileAds.Common;
using UnityEngine;

namespace GoogleMobileAds.Api
{
Expand All @@ -31,32 +31,44 @@ internal CustomNativeTemplateAd(ICustomNativeTemplateClient client)

public List<string> GetAvailableAssetNames()
{
return client.GetAvailableAssetNames();
return this.client.GetAvailableAssetNames();
}

public string GetCustomTemplateId()
{
return client.GetTemplateId();
return this.client.GetTemplateId();
}

// Get image asset corresponding to the key parameter of custom native template ad as a
// Texture2D. If the asset key does not map to an existing asset, a null object will be
// returned.
public Texture2D GetTexture2D(string key)
{
return Utils.GetTexture2DFromByteArray(client.GetImageByteArray(key));
byte[] imageAssetAsByteArray = this.client.GetImageByteArray(key);
if (imageAssetAsByteArray == null)
{
return null;
}

return Utils.GetTexture2DFromByteArray(imageAssetAsByteArray);
}

// Get text asset corresponding to the key parameter of custom native template ad as a
// string. If the asset key does not map to an existing asset, a null object will be
// returned.
public string GetText(string key)
{
return client.GetText(key);
return this.client.GetText(key);
}

public void PerformClick(string assetName)
{
client.PerformClick(assetName);
this.client.PerformClick(assetName);
}

public void RecordImpression()
{
client.RecordImpression();
this.client.RecordImpression();
}
}
}
4 changes: 2 additions & 2 deletions source/plugin/Assets/GoogleMobileAds/Common/DummyClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@

using System;
using System.Reflection;
using UnityEngine;

using GoogleMobileAds.Api;
using UnityEngine;

namespace GoogleMobileAds.Common
{
Expand All @@ -30,7 +30,7 @@ internal class DummyClient : IBannerClient, IInterstitialClient, IRewardBasedVid
public event EventHandler<EventArgs> OnAdClosed = delegate {};
public event EventHandler<Reward> OnAdRewarded = delegate {};
public event EventHandler<EventArgs> OnAdLeavingApplication = delegate {};
public event EventHandler<CustomNativeEventArgs> onCustomNativeTemplateAdLoaded = delegate {};
public event EventHandler<CustomNativeEventArgs> OnCustomNativeTemplateAdLoaded = delegate {};

public String UserId
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@
// limitations under the License.

using System;

using GoogleMobileAds.Api;

namespace GoogleMobileAds.Common
{
internal interface IAdLoaderClient
{
event EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;
event EventHandler<CustomNativeEventArgs> onCustomNativeTemplateAdLoaded;

event EventHandler<CustomNativeEventArgs> OnCustomNativeTemplateAdLoaded;

void LoadAd(AdRequest request);
}
}
30 changes: 21 additions & 9 deletions source/plugin/Assets/GoogleMobileAds/Editor/PostProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using UnityEditor.Callbacks;
using UnityEditor;
using UnityEditor.Callbacks;

#if (UNITY_5 && UNITY_IOS)
using UnityEditor.iOS.Xcode;
Expand All @@ -22,21 +22,33 @@ public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProj
iOSBuildTarget = BuildTarget.iPhone;
#endif

if(target == iOSBuildTarget)
if (target == iOSBuildTarget)
{
runPodUpdate(pathToBuiltProject);
RunPodUpdate(pathToBuiltProject);
}
}

static void runPodUpdate(string path)
public static void RunPodUpdate(string path)
{
#if !UNITY_CLOUD_BUILD
// Copy the podfile into the project.
string podfile = "Assets/GoogleMobileAds/Editor/Podfile";
string destpodfile = path + "/Podfile";
if(!System.IO.File.Exists(destpodfile))
string destPodfile = path + "/Podfile";

if (!System.IO.File.Exists(podfile))
{
UnityEngine.Debug.LogWarning(@"Could not locate Podfile in
Assets/GoogleMobileAds/Editor/");
return;
}

if (!System.IO.File.Exists(destPodfile))
{
FileUtil.CopyFileOrDirectory(podfile, destPodfile);
}
else
{
FileUtil.CopyFileOrDirectory(podfile, destpodfile);
FileUtil.ReplaceFile(podfile, destPodfile);
}

try
Expand All @@ -45,8 +57,8 @@ static void runPodUpdate(string path)
}
catch (Exception e)
{
UnityEngine.Debug.Log("Could not create a new Xcode project with CocoaPods: " +
e.Message);
UnityEngine.Debug.LogWarning("Could not create a new Xcode project with " +
"CocoaPods: " + e.Message);
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class AdLoaderClient : AndroidJavaProxy, IAdLoaderClient
private Dictionary<string, Action<CustomNativeTemplateAd, string>>
CustomNativeTemplateCallbacks { get; set; }
public event EventHandler<AdFailedToLoadEventArgs> OnAdFailedToLoad;
public event EventHandler<CustomNativeEventArgs> onCustomNativeTemplateAdLoaded;
public event EventHandler<CustomNativeEventArgs> OnCustomNativeTemplateAdLoaded;

public AdLoaderClient(AdLoader unityAdLoader) : base(Utils.UnityCustomNativeAdListener)
{
Expand Down Expand Up @@ -62,7 +62,7 @@ public void onCustomTemplateAdLoaded(AndroidJavaObject ad)
CustomNativeEventArgs args = new CustomNativeEventArgs() {
nativeAd = new CustomNativeTemplateAd(new CustomNativeTemplateClient(ad))
};
onCustomNativeTemplateAdLoaded(this, args);
OnCustomNativeTemplateAdLoaded(this, args);
}

void onAdFailedToLoad(string errorReason)
Expand Down
Loading

0 comments on commit 27ea48f

Please sign in to comment.