Creating an Android Plugin for Unity

I’ve had a request to do a tutorial on how to create Unity plugins, specifically for Android devices. The process can be a little tricky, it’s not brilliantly documented and that’s probably the reason why there aren’t too many plugins out there. Hopefully this tutorial will enable the creation of lots of useful plugins. A video of this tutorial can be found here.

A Quick Explanation of my Setup

For this tutorial, I’m using Eclipse under Windows, pretty standard stuff. However I’m using Blue Stacks as my emulator instead of the standard Android emulator. The main reason for this is that Blue Stacks acts more like an actual device and hence Mobclix will show its ad banners (it won’t in the standard emulator). Also it is wonderfully fast :-)

The Tutorial

So let’s get started, open Eclipse, create a new Android project, call it ‘MobclixTutorial’. Set the build target for Android 2.2 and specify a package name (i.e. com.platoevolved.moblixtutorial).

Now create a folder within the project called ‘libs’ and copy the mobclix.jar into it. This is the mobclix.jar that you can download from here. Right click this and choose ‘Add to Build Path’.

Open up the MobclixTutorialActivity.java file and amend as follows:

import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import com.mobclix.android.sdk.MobclixMMABannerXLAdView;
 
public class MobclixTutorialActivity extends Activity {
	private static FrameLayout layout;
	private static MobclixMMABannerXLAdView adView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        adView = new MobclixMMABannerXLAdView(this);
        layout = new FrameLayout(this);
        addContentView(layout,new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        layout.addView(adView, new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,android.view.Gravity.BOTTOM));
    }
}

So a quick explanation of the above code. We are creating a FrameLayout that fills the whole screen, and into that FrameLayout we are placing a Mobclix View (MobclixMMABannerXLAdView) that will gravitate to the bottom of the FrameLayout.

We now need to edit the AndroidManifest.xml file to look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.platoevolved.mobclixtutorial"
    android:versionCode="1"
    android:versionName="1.0" >
 
    <uses-sdk android:minSdkVersion="8" />
 
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MobclixTutorialActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
 
        <meta-data android:name="com.mobclix.APPLICATION_ID"
			android:value="insert-your-application-key"/>
        <activity
			android:name="com.mobclix.android.sdk.MobclixBrowserActivity"
			android:theme="@android:style/Theme.Translucent.NoTitleBar"
		/>
    </application>
	<uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.READ_PHONE_STATE" />
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    
</manifest>

This just adds some uses-permissions, meta-data and an activity needed by mobclix.

Now run the project on an android device and you should see the mobclix banner appear at the bottom of the screen.

Next, we need to convert this application into a library so that it will produce a jar that we can use from within the Unity app. So go into the properties of your app (select the Package Explorer and hit Alt Enter), select Android and tick ‘Is Library’. You will now see that this has created a mobclixtutorial.jar in the ‘bin’ folder.

Now we need to copy the ‘classes.jar’ (usually found somewhere like C:\Program Files (x86)\Unity\Editor\Data\PlaybackEngines\androiddevelopmentplayer\bin) from Unity into the ‘libs’ folder (and add it to the build path as before).

Now we need to change our code to extend the UnityPlayerActivity and also not try to use our xml layout file.

package com.platoevolved.mobclixtutorial;
 
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
 
import com.mobclix.android.sdk.MobclixMMABannerXLAdView;
import com.unity3d.player.UnityPlayerActivity;
 
public class MobclixTutorialActivity extends UnityPlayerActivity {
	private static FrameLayout layout;
	private static MobclixMMABannerXLAdView adView;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        adView = new MobclixMMABannerXLAdView(this);
        layout = new FrameLayout(this);
        addContentView(layout,new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        layout.addView(adView, new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,android.view.Gravity.BOTTOM));
    }
}

The Unity Part

Create a new Unity project called ‘mobclixtutorial’. Create a ‘Plugins’ folder and within this create another folder called ‘Android’. Copy the mobclixtutorial.jar and the mobclix.jar into this folder. Build the project for Android and then go into the ‘Temp\StagingArea’ folder with your project folder and copy the AndroidManifest.xml file into the ‘Plugins\Android’ folder. This manifest file now needs to be edited to look like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:installLocation="preferExternal" package="com.platoevolved.mobclixtutorial" android:versionName="1.0" android:versionCode="1">
  <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" />
  <application android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false">
    <activity android:name=".MobclixTutorialActivity" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
      <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.VideoPlayer" android:label="@string/app_name" android:configChanges="fontScale|keyboard|keyboardHidden|locale|mnc|mcc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|uiMode|touchscreen" android:screenOrientation="portrait">
    </activity>
 
	<meta-data android:name="com.mobclix.APPLICATION_ID"
			android:value="insert-your-application-key"/>
 
 
        <activity
			android:name="com.mobclix.android.sdk.MobclixBrowserActivity"
			android:theme="@android:style/Theme.Translucent.NoTitleBar"
		/>	
 
  </application>
  <uses-feature android:glEsVersion="0x00020000" />
  <uses-sdk android:minSdkVersion="6" android:targetSdkVersion="15" />
	<uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.READ_PHONE_STATE" />
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />    
 
</manifest>

At this point, you should be able to build this project for your Android device and (hopefully!?) see the mobclix ad banner at the bottom of your Unity application.

14 thoughts on “Creating an Android Plugin for Unity

  1. Thanks a lot! The simplicity of the example and the completeness and clarity of your description makes this a better tutorial than most out there.

  2. I got my ads showing on the eclipse android application, but when running the unity application I am getting only the empty scene, no ads…
    Could you give me any light on what may be wrong?

    • Sorry, only just spotted this (you can always email me direct if I’m a bit slow!), did you sort this out or do you still need help?

      • I got it working, I started a new project on eclipse and used the classes.jar from the androidplayer folder instead of the androiddevelopmentplayer. Or maybe I did something else wrong in the other attempt….
        but it is working now, thank you, you are amazing!

    • Admob has smartbanners that tend to fit intelligently to the size of the device. Unfortunately mobclix doesn’t seem to have an equivalent.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>