OpenAdAdapter plugin for Unity3D
Content
- OpenAdAdapter - simple management of mobile advertising
- The beginnings of OpenAdAdapter documentation
- OpenAdAdapter plugin for Unity3D
- Video: examples of using OpenAdAdapter
In a previous article I talked about my development of OpenAdAdapter . This is a library under the free license of Apache 2.0, designed to manage ad networks in mobile applications on iOS and Android. Its goal is to increase the monetization of the application and protect itself from a possible ban by redirecting traffic to other networks.
My next step is to create a plugin for Unity3D. I will tell you how to create a plugin and how to use the OpenAdAdapter plugin in Unity3D.
Plugin creation
I describe how to create a plugin for two reasons:
1. A library with source codes can still remain a black box if you do not want to spend hours and days studying it.
2. Similarly, you can create other plugins.
If you are not interested in how the plugin works, then go straight to the section “Adding an OpenAdAdapter to a Unity3D Project”.
The OpenAdAdapter plugin for Unity3D consists of 3 parts:
- C # class, through which the Unity3D program interacts with the adapter. It is located in the file Assets / OpenAdAdapter / OpenAdAdapter.cs. Also, this class performs specific functions for Unity3D, such as resizing the visible area of the screen (viewport) and the size of the interface elements so that the banner does not overlap the interface elements. I did not find a way to make it universal enough for all cases and I admit that the developer will have to adapt this class, since it works with one camera and one canvas in Render Mode: Screen Space - Camera.
- Objective-C code is an iOS layer for interacting with OpenAdAdapter on iOS.
- Android Library project, where they put OpenAdAdapter and Google Play Services.
Creating a plugin for iOS
1. Just copy all the sources (.h, .m), any (.a) and frameworks (.framework) to the Assets / Plugins / iOS folder . When Unity3D builds a project for iOS, an Xcode project will be created where all these files will be copied.
2. Open the Xcode project created in Unity3D, you need to make changes to this project, similar to what I described in the first article, namely:
add frameworks:
AdSupport.framework
StoreKit.framework
MessageUI.framework
libxml2.2.dylib
libz.dylib
libsqlite3.0.dylib
CoreTelephony.framework
EventKit.framework
EventKitUI.framework
Security.framework
Social.framework
WebKit.framework
Add -ObjC , to Other Linker Flags
In order not to make these changes every time you build under iOS, in Unity3D you can choose Append instead of Replace. (But at the time of the creation of the plugin, Unity3D 5.1 did not manage to run Append and had to return to Unity3D 5.0)
Creating a plugin for Android
The Android plugin for Unity3D is an eclipse project of the Android Library hosted in Assets / Plugins / Android.
For the advertising modules to work on the Android / GooglePlay platform, you need to add it to the Google Play Services project. I did this by simply copying the contents of the lib, res folders from the original Google Play Services project to my library project. Also copied OpenAdAdapter and ad networks files into the lib jar.
AndroidManifest.xml from the eclipse project will be merged with the main AndroidManifest.xml, so activity, permissions, meta tags will get into it.
Export plugin
The last step is to export everything to the package. (Assets - Export Package) I saved it as OpenAdAdapterWithGPS.unitypackage
Adding an OpenAdAdapter to a Unity3D Project
First, add the unity OpenAdAdapterWithGPS.unitypackage package to your project.
After adding you will have Assets / OpenAdAdapter / OpenAdAdapter.cs , Assets / OpenAdAdapter / OpenAdAdapterExample.unity and plugins in the corresponding folders Assets / Plugins / iOS and Assets / Plugins / Android .
The easiest and fastest way to start everything is to use the Assets / OpenAdAdapter / OpenAdAdapterExample.unity scene and everything should work on android, and on iOS, when you first open Xcode, you need to add frameworks:
AdSupport.framework
StoreKit.framework
MessageUI.framework
libxml2.2.dylib
libz.dylib
libsqlite3.0.dylib
CoreTelephony.framework
EventKit.framework
EventKitUI.framework
Security.framework
Social.framework
WebKit.framework
and add -ObjC to Other Linker Flags as described in the section “Creating a Plugin for iOS”.
Further I will describe what needs to be done if you use your scene, and not Assets / OpenAdAdapter / OpenAdAdapterExample.unity .
Create an Empty Object and name it OpenAdAdapter, drag the Assets / OpenAdAdapter / OpenAdAdapter.cs controller onto it .
An OpenAdAdapter object will have the Canvas property. Drag the Canvas UI onto the Canvas property of the OpenAdAdapter object.
In Canvas, select “Screen Space - Camera” Render Mode. And drag the Main Camera into the Render Camera property.
If your game has a more complex structure, more canvas or cameras, then you need to rewrite the Assets / OpenAdAdapter / OpenAdAdapter.cs controller to your needs.
The Assets / OpenAdAdapter / OpenAdAdapter.cs controller has auxiliary non-static methods with the Cmd * prefix (for example, CmdShowTopBanner), they are added so that they can be attached as event handlers to buttons in the Assets / OpenAdAdapter / OpenAdAdapterExample.unity example .
JSON files with ad network IDs are registered in Assets / OpenAdAdapter / OpenAdAdapter.cs and you must change them to yours.
Examples
Initialization (this code is called when starting from the Update method of the OpenAdAdapter.cs controller)
#if UNITY_IPHONE
OpenAdAdapter.Init ("https://raw.githubusercontent.com/sample-data/oad1/master/ios-redir.json");
#endif
#if UNITY_ANDROID
OpenAdAdapter.Init ("https://raw.githubusercontent.com/sample-data/oad1/master/android-redirect.json");
#endif
Show banner
OpenAdAdapter.ShowTopBanner ();
Hide banner
OpenAdAdapter.HideBanner ();
Show video ads
OpenAdAdapter.Video ();
More usage examples can be found in the OpenAdAdapter.cs controller and a description of the methods on the wiki page .