This series of post outlines the development of Android plugins for Unity3D. I had been asked to develop Android plugins for Unity3D every few days or months, each time following similar paths which I think would be useful to those who will do the same job. And here is how.

Clone the source code in GitHub here !

Tools you need:

Android Studio

Unity3D

Android Devices for testing

Some patience to go through this post

Let’s get started.

The 1st example is extremely simple. We define a function DoSthInAndroid() that dump some information using Java Log.i() function, and get this function called in Unity3D. Though simple enough, it outlines all the required procedures, and are very useful to understand the entire workflow.

Start Android Studio, create an Android project (AndroidAddin) with an Empty Activity.

In Android Studio, create another library project (AndroidLib) by clicking the menu File > New > New Module

Select “Android Libray” in the wizard

Build the project, make sure everything works so far.

In the AndroidLib module, add a class called “Helper”

In the Helper class add a function DoSthInAndroid()



package com.androidaddin.androidlib;

import android.util.Log;

public class Helper {

public static void DoSthInAndroid()

{

Log.i(“Unity”, “Hi, Sth is done in Android”);

}

}

To make sure this work, in the main app, add below testing function:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Helper.DoSthInAndroid(); // Testing

}

And when you run the Android App, you can see the output in Logcat window:

… 15:59:57.276 12121-12121/com.androidaddin.androidaddin I/Unity: Hi, Sth is done in Android

If you explicitly build the AndroidLib project only, you will find below output:

The aar file is the compiled android archive or the binary distribution of an Android Library Project.

Rename the *-debug.aar as zip file, unzip the file, you will see a file called “classes.jar” which contains only java code files (classes):

Grab the classes.jar for later use in Unity3D.

Open Unity3D, Create a project, then create a folder in Assets: Assets\Plugins\Android

Add the classes.jar to the above Assets\Plugins\Android folder

Create a new MonoBehaviour script TestAndroidPlugin.cs, and attach it to the camera:

public class TestAndroidPlugin : MonoBehaviour { void Start ( ) { Debug.Log("Android function is to be called"); var ajc = new AndroidJavaClass ( "com.androidaddin.androidaddin.Helper" ) ; //(1) ajc . CallStatic ( "DoSthInAndroid" ) ; //(2) Debug.Log("Android function is called"); } }

The AndroidJavaClass offers access to Java class object, and the CallStatic fires the java object’s method. Simple?

Save the scene, add the scene in the build setting dialog, remember to change the BundleID. Build the Unity app and run it on your device, you will see in the Logcat window what you expect.

Yeah! We have stepped the most important stride in developing Android plugins for Unity3D. There are more cool stuff than calling a log() function in Unity3D, we can do quite a lot such as adding Android UI (button, image, textview, progress bar etc.) to Unity scene, which will be discussed in a separate post!

To summarize:

You need define something in Android java which is to be called in Unity3D C#; You need compile the java class into a jar or aar file (shall talk about this in directly using aar in another post); You need put the compiled jar/aar in a special folder (Assets\Plugins\Android), well not always! See my others posts in this blog! You need call the java function with the aid of AndroidJavaClass or AndroidJavaObject class which is defined in Unity3D.

UPDATE: In Unity3D 5.0+, you no longer need unpack the aar file and manually get jar file.

UPDATE: In Unity3D 5.0+, you no longer need put the file in Plugins\Android folder, anywhere in the Asset folder will do!.



You might feel cumbersome in renaming the aar –> zip –> copy files to Unity3D –> Build app etc. Yes, this is indeed tedious! In the following posts, I shall continue to propose solutions to automate this process, and everything is fine!

Here is the 2nd part of this tutorial series: Step-by-Step guide for developing Android Plugin for Unity3D (II)

Enjoy and happy new year! See you in 2016! Clone the source code in GitHub here !