Wednesday, September 25, 2013

Android Plug-in for Unity3d - Part 1

Standard
Hello All:

In this two parts post I will explain how to create an android plugin to access native APIs of android or to call 3rd party libraries like ads integration. Basic information on how to create android plug-in can be found here.


This articles assumes that android development environment is setup beforehand.
In 1st part I will show you how to call java methods from C# in Unity3d.
Unity3d can communicate to native android object using AndroidJNI. Creating plug-in in Unity3d involves two steps

  1. Creating JAR file which will have all java code to interact with Android
  2. C# wrapper which will call the methods of the jar created in #1
Let us start with Step #1

Update: This step has been updated to use Android Studio. Check this post.
  1. Create an android project with default settings. The project will look something like this

  2. We will delete MainActivity.java as for this tutorial we will not use Android Activity class
  3. Now create two classes
    1. GreetingsHelper.java
    2. ToastHelper.java

  package com.ashwanik.androidpluginpart1;

public class GreetingsHelper {

 public String GetGreetings() {
  return "Hello there!!!!";
 }

}

    

   package com.ashwanik.androidpluginpart1;

import android.app.Activity;
import android.widget.Toast;

public class ToastHelper {

 public static void ShowToast(final Activity activity, final String message) {
  activity.runOnUiThread(new Runnable() {

   @Override
   public void run() {
    Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();

   }

  });
 }

}

Now we will create .jar with these two classes.

Right click on project name and select Export. Then select JAR file and click on Next

Now select items as shown in the diagram below. For this example we do not need Res / Lib/ Asset folders as they will just increase the size of the jar file.

Give the location to save the file and click Finish
In order to let Unity3d know that we have plug-in to use, we need to put our .jar in a particular folder.
Create this folder structure in your unity project
                                            Assets-->Plugins-->Android

and copy .jar file in Android folder. Now we are ready to test this.



Add a new C# script file to Unity3d project and paste below code in that

 
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;
using Rand = UnityEngine.Random;
using Obj = UnityEngine.Object;

public class AndroidPluginTest : MonoBehaviour
{

    #region Variables
    string labelText;
    #endregion

    #region Methods
    void OnGUI()
    {
        GUI.Label(new Rect(100, 100, 200, 100), labelText);

        if (GUI.Button(new Rect(100, 300, 200, 100), "Get Greetings"))
        {
            labelText = GetGreetings();
        }

        if (GUI.Button(new Rect(100, 500, 200, 100), "Show Toast"))
        {
            ShowToast("Toast is being shown");
        }
    }

    String GetGreetings()
    {
        AndroidJavaObject plugin = new AndroidJavaObject("com.ashwanik.androidpluginpart1.GreetingsHelper");
        String message = plugin.Call<string>("GetGreetings");
        return message;
    }

    void ShowToast(string message)
    {
        AndroidJavaClass plugin = new AndroidJavaClass("com.ashwanik.androidpluginpart1.ToastHelper");
        AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject >("currentActivity");
        plugin.CallStatic("ShowToast", activity, message);
    }
    #endregion
}

Attach this script to MainCamera and save the scene.

Now connect Android Mobile and hit Build And Run. 
You will see two buttons and clicking on them will give you desired output.
You can download the sample project from here for reference.

In the next post, I will explain how to extend UnityPlayerActivity in android Plugin.
Hope this helps.



Thanks for printing this post. Hope you liked it.
Keep visiting and sharing.
Thanks,
Ashwani.

3 comments :

  1. It's a simple and very useful tutorial.

    ReplyDelete
  2. Hi, do you now how to create these type of plugin with Unity in the iOS platform?

    ReplyDelete
  3. We don't need manifest file???

    ReplyDelete