📜 ⬆️ ⬇️

OpenAdAdapter plugin for Unity3D

Content


- OpenAdAdapter - simple management of mobile advertising
- The rudiments of OpenAdAdapter documentation
- OpenAdAdapter plugin for Unity3D
- Video: OpenAdAdapter usage examples

In the last article I talked about my development of the OpenAdAdapter . This is a free Apache 2.0 library for managing ad networks in iOS and Android mobile apps. Its goal is to increase the monetization of the application and to protect against 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.

Creating a plugin


I describe how to create a plugin for two reasons:
')
1. A library with source codes can still remain a black box, if there is no desire to spend hours and days on its study.
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 the Unity3D Project”.

The OpenAdAdapter plugin for Unity3D consists of 3 parts:

- C # class through which the program on Unity3D interacts with the adapter. It is located in the Assets / OpenAdAdapter / OpenAdAdapter.cs file . Also this class performs specific functions for Unity3D, such as changes in the size of 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, as it works with one camera and one canvas in Render Mode: Screen Space - Camera.

- Objective C code is an iOS layer for interacting with an OpenAdAdapter on iOS.

- Android Library project, where the OpenAdAdapter and Google Play Services are thrown.

Creating a plugin for iOS

1. Just copy all the source files (.h, .m), or (.a) and frameworks (.framework) to the Assets / Plugins / iOS folder. When Unity3D builds a project under 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 select Append instead of Replace. (But at the time of creating the plug-in, Unity3D 5.1 could not cope with the execution of Append and had to return to Unity3D 5.0)

Creating a plug-in for Android

The Android plugin for Unity3D is the eclipse project of the Android Library, hosted in Assets / Plugins / Android.

For advertising modules to work on the Android / GooglePlay platform, you need to add 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 into my library project. Also copied into the lib jar files OpenAdAdapter and ad networks.

The AndroidManifest.xml of the eclipse project will be merged with the main AndroidManifest.xml, so the activity, permissions, meta tags will be included into it.

Export plugin



The last step is to export everything to pekedzh. (Assets - Export Package) I saved it under the name OpenAdAdapterWithGPS.unitypackage

Adding an OpenAdAdapter to a Unity3D project



First of all, add unity pekedizh OpenAdAdapterWithGPS.unitypackage to your project.



After adding you will have Assets / OpenAdAdapter / OpenAdAdapter.cs , Assets / OpenAdAdapter / OpenAdAdapterExample.unity and plugins in the corresponding Assets / Plugins / iOS and Assets / Plugins / Android folders.



The easiest and fastest way to start everything is to use the Assets / OpenAdAdapter / OpenAdAdapterExample.unity scene and everything should work on the 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 it was described in the section “Creating a plugin for iOS”.

Next I will describe what needs to be done if you use your scene, and not Assets / OpenAdapter / OpenAdAdapterExample.unity .

Create an Empty Object and name it OpenAdAdapter, drag the controller Assets / OpenAdapter / OpenAdAdapter.cs onto it .

The OpenAdAdapter object will have a Canvas property. Drag the user interface's Canvas 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.

If your game has a more complex structure, more kanvasov or cameras, then you need to rewrite the controller Assets / OpenAdAdapter / OpenAdAdapter.cs to fit 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 the buttons in the Assets / OpenAdapter / OpenAdAdapterExample.unity example .

JSON files with ad network IDs are registered in Assets / OpenAdAdapter / OpenAdAdapter.cs and you should change them to your own.

Examples


Initialization (this code is called when it is launched from the Update method of the controller OpenAdAdapter.cs)

  #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 examples of usage can be found in the controller OpenAdAdapter.cs and the description of the methods on the wiki page.

Source: https://habr.com/ru/post/264315/


All Articles