Sunday, September 29, 2013

Android Plug-in for Unity3d - Part 2

Hello All:

Sometimes we want to extend the default behavior of Activity which come with Unity3d. In this post I will explain how to extend UnityPlayerActivity. This post is continuation of this.If you did not see the post please go through it I will be here :).

As mentioned in part 1, 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

Create new android application project with default settings.

UnityPlayerActivity comes with Unity3d in classes.jar package. This package can be found at

  1. On Windows: %Program Files%\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\bin
  2. On Mac: /Applications/Unity/PlaybackEngines/AndroidPlayer/bin
Add classes.jar to libs folder. Create a new class AndroidPluginPart2Activity  and extend it from UnityPlayerActivity

package com.ashwanik.androidpluginpart2;

import com.unity3d.player.UnityPlayerActivity;

import android.os.Bundle;
import android.widget.Toast;

public class AndroidPluginPart2Activity extends UnityPlayerActivity {

 protected void onCreate(Bundle savedInstanceState) {


 public String GetGreetings() {
  return "Hello There!!!";

 public void ShowToast(final String message) {
  this.runOnUiThread(new Runnable() {

   public void run() {
    Toast.makeText(AndroidPluginPart2Activity.this, message,



Export .jar file with following settings

Create unity3d project to test this plug-in. Add the generated .jar to Assets/Plugins/Android folder.
Create a script file and paste following code in it

    using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;
using System.Linq;

public class AndroidPluginTest : MonoBehaviour

    #region Variables
    string labelText;

    #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()
        return GetCurrentActivity().Call

    void ShowToast(string message)
        GetCurrentActivity().Call("ShowToast", message);

    AndroidJavaObject GetCurrentActivity()
        AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        return activity;



Attach this script to main camera and save the scene.

Now we need to tell Unity to use our activity as main activity. For this copy default androidmanifest.xml which comes with Untiy3d to Assets/Plugins/Android folder

Location of default AndroidManifest.xml
  1. On Windows: %Program Files%\Unity\Editor\Data\PlaybackEngines\AndroidPlayer\
  2. On Mac: /Applications/Unity/PlaybackEngines/AndroidPlayer/
Update AndroidManifest.xml as below






This is it.

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

Hope this helps.


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


  1. Hi,
    I am following your tutorial for plugin in second part as you told we need to make our activity as main activity. If we need other activity as main activity. what we need to do to use other activity as main activity

  2. this tutorial is i want to integrate google maps in unity....if u have tutorial on it...plz share it on.... email-