Skip to content

Commit

Permalink
Adding v1 of a Google Play services-compatible unity plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Leichtenschlag committed Feb 20, 2014
1 parent a08f98b commit 9d9cb28
Show file tree
Hide file tree
Showing 9 changed files with 691 additions and 0 deletions.
33 changes: 33 additions & 0 deletions unity/android/Assets/Plugins/Android/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" package="com.example.admobtest" android:versionName="1.0" android:versionCode="1">
<supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
<!-- Google Mobile Ads Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">
<!-- meta-data tag for Google Play services -->
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
<activity android:name="com.unity3d.player.UnityPlayerProxyActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" >
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" >
</activity>
<activity android:name="com.unity3d.player.UnityPlayerNativeActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" >
<meta-data android:name="unityplayer.ForwardNativeEventsToDalvik" android:value="true" />
<meta-data android:name="android.app.lib_name" android:value="unity" />
</activity>
<activity android:name="com.unity3d.player.VideoPlayer" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" >
</activity>
<!-- Google Mobile Ads Activity -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:label="@string/app_name"
android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen">
</activity>
</application>
<uses-feature android:glEsVersion="0x00020000" />
<uses-sdk android:minSdkVersion="9" android:targetSdkVersion="19" />
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using UnityEngine;

// Example script showing how you can easily call into the GoogleMobileAdsPlugin.
public class GoogleMobileAdsDemoScript : MonoBehaviour {

void Start()
{
print("Started");
GoogleMobileAdsPlugin.CreateBannerView("ca-app-pub-9380760713574559/3453176828",
GoogleMobileAdsPlugin.AdSize.SmartBanner,
true);
print("Created Banner View");
GoogleMobileAdsPlugin.RequestBannerAd(true);
print("Requested Banner Ad");
}

void OnEnable()
{
print("Registering for AdMob Events");
GoogleMobileAdsPlugin.ReceivedAd += HandleReceivedAd;
GoogleMobileAdsPlugin.FailedToReceiveAd += HandleFailedToReceiveAd;
GoogleMobileAdsPlugin.ShowingOverlay += HandleShowingOverlay;
GoogleMobileAdsPlugin.DismissedOverlay += HandleDismissedOverlay;
GoogleMobileAdsPlugin.LeavingApplication += HandleLeavingApplication;
}

void OnDisable()
{
print("Unregistering for AdMob Events");
GoogleMobileAdsPlugin.ReceivedAd -= HandleReceivedAd;
GoogleMobileAdsPlugin.FailedToReceiveAd -= HandleFailedToReceiveAd;
GoogleMobileAdsPlugin.ShowingOverlay -= HandleShowingOverlay;
GoogleMobileAdsPlugin.DismissedOverlay -= HandleDismissedOverlay;
GoogleMobileAdsPlugin.LeavingApplication -= HandleLeavingApplication;
}

public void HandleReceivedAd()
{
print("HandleReceivedAd event received");
}

public void HandleFailedToReceiveAd(string message)
{
print("HandleFailedToReceiveAd event received with message:");
print(message);
}

public void HandleShowingOverlay()
{
print("HandleShowingOverlay event received");
}

public void HandleDismissedOverlay()
{
print("HandleDismissedOverlay event received");
}

public void HandleLeavingApplication()
{
print("HandleLeavingApplication event received");
}
}

114 changes: 114 additions & 0 deletions unity/android/Assets/Plugins/GoogleMobileAds/GoogleMobileAdsPlugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
using System;
using UnityEngine;

// The Google Mobile Ads script used to call into the native Google Mobile Ads Plugin library.
public class GoogleMobileAdsPlugin : MonoBehaviour {

// The plugin's class name.
private const string PluginClassName = "com.google.unity.GoogleMobileAdsPlugin";

// Defines string values for supported ad sizes.
public class AdSize
{
private string adSize;
private AdSize(string value)
{
this.adSize = value;
}

public override string ToString()
{
return adSize;
}

public static AdSize Banner = new AdSize("BANNER");
public static AdSize MediumRectangle = new AdSize("IAB_MRECT");
public static AdSize IABBanner = new AdSize("IAB_BANNER");
public static AdSize Leaderboard = new AdSize("IAB_LEADERBOARD");
public static AdSize SmartBanner = new AdSize("SMART_BANNER");
}

// These are the ad callback events that can be hooked into.
public static event Action ReceivedAd = delegate {};
public static event Action<string> FailedToReceiveAd = delegate {};
public static event Action ShowingOverlay = delegate {};
public static event Action DismissedOverlay = delegate {};
public static event Action LeavingApplication = delegate {};

void Awake()
{
gameObject.name = this.GetType().ToString();
SetCallbackHandlerName(gameObject.name);
DontDestroyOnLoad(this);
}

// Create a banner view and add it into the view hierarchy.
public static void CreateBannerView(string publisherId, AdSize adSize, bool positionAtTop)
{
AndroidJavaClass playerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject activity = playerClass.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaClass pluginClass = new AndroidJavaClass(PluginClassName);
pluginClass.CallStatic("createBannerView",
new object[4] { activity, publisherId, adSize.ToString(), positionAtTop });
}

// Request a new ad for the banner view without any extras.
public static void RequestBannerAd(bool isTesting)
{
AndroidJavaClass pluginClass = new AndroidJavaClass(PluginClassName);
pluginClass.CallStatic("requestBannerAd", new object[1] {isTesting});
}

// Request a new ad for the banner view with extras.
public static void RequestBannerAd(bool isTesting, string extras)
{
AndroidJavaClass pluginClass = new AndroidJavaClass(PluginClassName);
pluginClass.CallStatic("requestBannerAd", new object[2] {isTesting, extras});
}

// Set the name of the callback handler so the right component gets ad callbacks.
public static void SetCallbackHandlerName(string callbackHandlerName)
{
AndroidJavaClass pluginClass = new AndroidJavaClass(PluginClassName);
pluginClass.CallStatic("setCallbackHandlerName", new object[1] {callbackHandlerName});
}

// Hide the banner view from the screen.
public static void HideBannerView()
{
AndroidJavaClass pluginClass = new AndroidJavaClass(PluginClassName);
pluginClass.CallStatic("hideBannerView");
}

// Show the banner view on the screen.
public static void ShowBannerView() {
AndroidJavaClass pluginClass = new AndroidJavaClass(PluginClassName);
pluginClass.CallStatic("showBannerView");
}

public void OnReceiveAd(string unusedMessage)
{
ReceivedAd();
}

public void OnFailedToReceiveAd(string message)
{
FailedToReceiveAd(message);
}

public void OnPresentScreen(string unusedMessage)
{
ShowingOverlay();
}

public void OnDismissScreen(string unusedMessage)
{
DismissedOverlay();
}

public void OnLeaveApplication(string unusedMessage)
{
LeavingApplication();
}
}

Binary file not shown.
Binary file added unity/android/GoogleMobileAdsPlugin.unitypackage
Binary file not shown.
147 changes: 147 additions & 0 deletions unity/android/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
About
=====
Copyright 2014 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=====

Features
--------

This is the Google Mobile Ads Unity Plugin for Android. It provides a way to
request Google Mobile ads from a Unity project. This plugin was written and
tested with Google Play Services 4.1 for Android, and Unity 4.3.0f4.

The plugin contains a .unitypackage file for those that want to easily import
the plugin, as well as the source code for those that want to iterate on it.

Requirements:
-------------

* Unity 4.3 (untested on previous versions)
* Google Play services library, revision 13 or higher
* An ad unit ID

Directions for importing the plugin:
------------------------------------

1. Open your project in the Unity editor.
2. On the top toolbar, select "Assets" -> "Import Package" -> "Custom Package".
3. Select the GoogleMobileAdsPlugin.unitypackage file.
4. Import all of the files for the plugins by selecting "Import". Make sure
to check for any conflicts with files.
5. Drag the GoogleMobileAds prefab from the Plugins/GoogleMobileAds/ folder into
your Unity scene.

Note: If you already have an AndroidManifest.xml in Plugins/Android/, you can
just add the necessary activities and permissions to your existing manifest as
explained in https://developers.google.com/mobile-ads-sdk/docs#play instead of
importing the manifest from the .unitypackage.

Additional dependencies:

1. Add the google-play-services_lib folder,
located at <android-sdk>/extras/google/google_play_services/libproject,
into the Plugins/Android folder of your project.

This plugin also comes with an example plugin usage script, which is already
attached to the GoogleMobileAdsPlugin component for convenience. Simply edit
Plugins/GoogleMobileAdsPlugin/GoogleMobileAdsDemoScript.cs and include your ad
unit ID and run your project to see plugin working.


Google Mobile Ads Unity Plugin API
==================================
The plugin provides the following methods:

1. CreateBannerView

Takes in a publisherId string as well as a constant for the ad size. The
last boolean parameter denotes whether the ad should be shown at the top
or bottom of the screen.

An example call placing the ad at the top of the screen is provided below:

GoogleMobileAdsPlugin.CreateBannerView("INSERT_YOUR_AD_UNIT_ID_HERE",
GoogleMobileAdsPlugin.AdSize.Banner,
true);
2. RequestBannerAd

Takes in a testing flag as well as an optional string representing a list
of extras. If you don't have any extras, you can request a live ad with:

GoogleMobileAdsPlugin.RequestBannerAd(false);

An example call requesting a test ad with some extras is shown below:

string extras = "{\"color_bg\":\"AAAAFF\", \"color_text\":\"FFFFFF\"}";
GoogleMobileAdsPlugin.RequestBannerAd(true, extras);

NOTE: Make sure to use correctly formed JSON when passing an extras string.
If malformed JSON is passed, the extras will be ignored.

3. HideBannerView

Called after a BannerView has been created, this method can hide the ad from
showing on screen. An example call of this is shown below:

GoogleMobileAdsPlugin.HideBannerView();

4. ShowBannerView

Called after a BannerView has been created, this method can show any ad that
has been hidden. An example call of this is shown below:

GoogleMobileAdsPlugin.ShowBannerView();

This plugin also allows you the option to listen for ad events. The following
events are supported:

public static event Action ReceivedAd;
public static event Action<string> FailedToReceiveAd;
public static event Action ShowingOverlay;
public static event Action DismissedOverlay;
public static event Action LeavingApplication;

Registering for an event can be done using the += operater as is shown below:

// Assume HandleReceivedAd is your function.
GoogleMobileAdsPlugin.ReceivedAd += HandleDidReceiveAd;

Remember to un-register for events when you're cleaning up your GameObjects.
You can unregister using the -= operator as is shown below:

// Assume HandleReceivedAd is your function.
GoogleMobileAdsPlugin.ReceivedAd -= HandleDidReceiveAd;


Updating the plugin
-------------------

The plugin's .unitypackage only includes the compiled jar from the library
project. If you want to make changes to the library or see the source code, you
can find the project at
https://github.com/googleads/googleads-mobile-plugins/tree/master/unity.

The library project depends on Unity's classes.jar, which can be found at
/Applications/Unity/Unity.app/Contents/PlaybackEngines/AndroidDevelopmentPlayer/bin
on Mac and usually at
C:\Program Files\Unity\Editor\Data\PlaybackEngines\AndroidDevelopmentPlayer\bin
on Windows.

Additional Resources
--------------------
https://developers.google.com/mobile-ads-sdk/docs
https://groups.google.com/group/google-admob-ads-sdk
https://plus.google.com/+GoogleAdsDevelopers

20 changes: 20 additions & 0 deletions unity/android/plugin-library/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.google.unity"
android:versionName="1.0"
android:versionCode="1">
<uses-sdk android:minSdkVersion="9"
android:targetSdkVersion="19" />
<!-- Google Mobile Ads Permissions -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application>
<!-- Denote the referenced Google Play services version -->
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<!-- Google Mobile Ads Activity -->
<activity android:name="com.google.android.gms.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>

16 changes: 16 additions & 0 deletions unity/android/plugin-library/project.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# This file is automatically generated by Android Tools.
# Do not modify this file -- YOUR CHANGES WILL BE ERASED!
#
# This file must be checked in Version Control Systems.
#
# To customize properties used by the Ant build system edit
# "ant.properties", and override values to adapt the script to your
# project structure.
#
# To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home):
#proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt

android.library=true
# Project target.
target=android-19
android.library.reference.1=../../libproject/google-play-services_lib
Loading

0 comments on commit 9d9cb28

Please sign in to comment.