Script.it .Nam Myoho Renghe Kyo.

Script.it
[Unity3D] Writing native plugins: Android

These days, it’s all about Unity3D and it’s ability to crosscompile to different platforms.
This gives developers and creatives an quick and easy creation flow. There are lots of resources online whom explain in detail the workings of Unity3D and its components…. but you don’t see alot of talks about the native plugins for Android and IOS. So thats what i am going to cover here.

So let’s start with the easy one,


Android native plugins for Unity3D.

To get your plugin development started, download a java / android development tool.
In this case i use Eclipse. Make sure you have your Android Developer Tools downloaded and linked in the editor.
I won’t go into the nitty gritty on this subject as i assume it’s not that hard to accomplish.

Now that your environment is setup, go ahead and create a new Android application project and mark it as library like you see below.

1. Create project.
android_create_project


2. Give namespace.
android_namespace


3. Set as library.
android_mark_as_library

Complete your project creation as normal. Blank activity is fine.

We are now ready to create our plugin.
Start by creating a “bridge” class.
This class will be the main entry point of our Unity Native Android plugin and must extend android.app.Application for it to work properly.

Below is a code stub of a basic bridge class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Basic native plugin bridge.
package com.rackdoll.mynativeplugin;
 
import android.app.Application;
import android.content.Context;
 
public class Bridge extends Application{
 
    //Native plugin method.
    public void DoMyNativePluginStuff( Context ctx ){
 
        // a context is passed as this will be context independant.
 
    }
}
Because we have set our project to library, Eclipse will build a .jar file in the bin folder automatically. If not, just try and run the project.
This jar file is all you will need inside Unity3D.


How do we implement this in Unity3D ?


Assuming you already have a project created in Unity3D,
make sure there is a “Plugins” folder in your “Assets” folder. In this folder, create a subfolder named “Android”, so you will have the following structure: Assets/Plugins/Android. In this folder you can place your library .jar file.

Let’s starting implementing the C# code in our Unity3D project.
1. Create a C# script and open it in the mono development environment.
2. Add the following code to enable you to call the native plugin:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
//Get the Unity3D player object.
AndroidJavaClass unityPlayerObject = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
 
//Get the Unity3D android context/activity.
AndroidJavaObject unityActivity    = unityPlayerObject.GetStatic<AndroidJavaObject>("currentActivity");
 
//Setup parameters to pass.
object[] parameters = new object[1];
parameters [0] = unityActivity;
 
//Setup connection to the android plugin	
AndroidJavaObject ajo = new AndroidJavaObject ("com.rackdoll.mynativeplugin.Bridge");
 
//Call android plugin method.
ajo.Call("DoMyNativePluginStuff", parameters);

There you go! Now you can do some native Android ownage from Unity3D.

What about passing other parameters to the plugin?
1. Add more parameters to your Java class, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//Basic native plugin bridge.
package com.rackdoll.mynativeplugin;
 
import android.app.Application;
import android.content.Context;
 
public class Bridge extends Application{
 
    //Native plugin method.
    public void DoMyNativePluginStuff( Context ctx, String mytext ){  // <----- String parameter added.
 
        // a context is passed as this will be context independant.
 
    }
}

Adding a String as parameter.

2. Expand the Unity3D c# call to the native plugin like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 
//Get the Unity3D player object.
AndroidJavaClass unityPlayerObject = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
 
//Get the Unity3D android context/activity.
AndroidJavaObject unityActivity    = unityPlayerObject.GetStatic<AndroidJavaObject>("currentActivity");
 
//Setup parameters to pass.
object[] parameters = new object[2];
parameters [0] = unityActivity;
parameters [1] = "my passed string";   // <----- extra string to pass.
 
//Setup connection to the android plugin	
AndroidJavaObject ajo = new AndroidJavaObject ("com.rackdoll.mynativeplugin.Bridge");
 
//Call android plugin method.
ajo.Call("DoMyNativePluginStuff", parameters);

Simple and straightforward.

Happy coding \o.
Rackdoll

Creative Commons License
[Unity3D] Writing native plugins: Android by Script.it, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Netherlands License.

Comments are closed.

Categories
Archives
Live Supporters