Unity Android Plugin

1.Create a new Android application in Eclipse

  • Without a theme (theme should be “None”)
  • Don’t create an “Activity” or a custom launcher icon

2.Add unity JARs

  • The jars are located at …Unity\Editor\Data\PlaybackEngines\Androidplayer\bin\classes.jar
  • Right click on project go to Properties => Java Build Path => Libraries => Add External JARS then give the unity classes.jar path, click Order and Export tab and check classes.jar and click ok

3.Code and Export Jar

  • Now we can import unity to our class using the line “import com.unity3d.player.UnityPlayer;”. Following are the class that I have written.


package com.myuselesswork.plugin;

import java.util.GregorianCalendar;
import com.unity3d.player.UnityPlayer;

import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class Yapee {

private Context context;

Yapee(){

context = UnityPlayer.currentActivity.getBaseContext();
}

public void Shedule(int x){

Long time = new GregorianCalendar().getTimeInMillis()+x;

Intent intentAlarm = new Intent(context, AlarmReciver.class);

AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);

alarmManager.set(AlarmManager.RTC_WAKEUP,time, PendingIntent.getBroadcast(context,1000, intentAlarm, PendingIntent.FLAG_UPDATE_CURRENT));

}

public void ClearNotification(){

NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

nm.cancelAll();
}

}

package com.myuselesswork.plugin;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class AlarmReciver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

PendingIntent p = PendingIntent.getActivity(context, 0, new Intent(), 0);
NotificationManager nm = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);

Notification n = new Notification(
R.drawable.ic_launcher,
"Task Burnt down!!!",
System.currentTimeMillis()
);
n.flags |= Notification.FLAG_AUTO_CANCEL;
CharSequence from = "Task Burner";
CharSequence message = "Task Burnt!!Now you are free";

n.setLatestEventInfo(context, from, message, p);

n.vibrate = new long[] { 100, 250, 100, 500};

nm.notify(1000, n);

}

}

  • Right click on project go to Export => JAVA => JAR => Next  then give a path and finish.

4.Add the plugin to unity

  • Put the JAR file in the Assets\Plugins\Android folder in unity project
  • Now we can access functions in the plugin.Following  are some sample code

private AndroidJavaObject m_AndroidJavaObject;

…..

m_AndroidJavaObject = new AndroidJavaObject("com.myuselesswork.plugin.Yapee");

…..

//Calling a method

m_AndroidJavaObject.Call("ClearNotification");

…..

//Calling  a method with parameters

int x =1000;
m_AndroidJavaObject.Call("Shedule",new object[]{x});

  • If we need to edit the AndroidManifest.xml file we have to put the edited file in to the Assets\Plugins\Android folder

A sample project which uses this plugin: https://play.google.com/store/apps/details?id=com.myuselesswork.taskburner&hl=en

AndroidManifest

Note : This a simple sample code for a plugin to make the phone vibrate via android call  (for Makluad :)) https://drive.google.com/file/d/0ByoByqm47PspdmVSLUFqUUotams/view?usp=sharing

16 comments

  1. Hi! Thanks for your post, but it doesn’t work. I try to add notifications to my unity game, made everything like in this post, but notifications are not sent to the task bar. Your “task burn” application works, but my sample not.

    What I do:
    1. make two .java files with those two classes.
    2. make a .jar file, put it to the Assets/Plugins/Android.
    3. modify manifest – add a line

    4. add this code to the GUI.Button

    private AndroidJavaObject m_AndroidJavaObject = null;

    if (m_AndroidJavaObject == null)
    {
    m_AndroidJavaObject = new AndroidJavaObject(“com.myuselesswork.plugin.Yapee”);
    }

    m_AndroidJavaObject.Call(“ClearNotification”);

    int x = 1000;
    m_AndroidJavaObject.Call(“Shedule”, new object[] { x });

    I press the button, nothing happens.

    Please help me to solve the problem.
    Thanks!

    Like

      1. Yes, I told that but forgot to show the line:

        I put it to the node manifest/application right before the first activity node.

        Like

      2. More of that, I tried other different samples, nothing works. Maybe I do the .jar file wrong? I use cmd “jar cf ” for that.

        Like

  2. I m not sure whether the problem is with jar file. If your code is working perfectly i think its the AndroidManifest.xml file.Anyway i created the jar file using eclipse (File==>Export then you’ll get a Export popup, select Java==>Jar) ,
    And I ve attached the AndroidManifest.xml(please change the extension to xml after downloading)file that I ve used to check the plugin.

    Like

  3. It works! but how i can start my application, when clicking the notification? i tried to create new Intent, but it doesn’t work.

    Intent notificationIntent = new Intent(
    // Here i Tried to use different parameters
    );
    notificationIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
    Intent.FLAG_ACTIVITY_SINGLE_TOP |
    Intent.FLAG_ACTIVITY_NEW_TASK);
    PendingIntent p = PendingIntent.getActivity(context, 0, notificationIntent , 0);

    Like

  4. This is a big “good job” 🙂 . But i can’t make this run properly. Can you publish all the work as a git repository or a zip file? This would be really great 🙂

    Like

  5. Guys, did you really make it work? I didn’t. I have tried different manifest combinations (activities, permissions etc), but nothing happens. I press the notify buttons and it doesn’t appear.

    Like

      1. No.. Sorry for that. I have an error java.lang.ClassNotFoundException when trying to get the object or the class in my c# code. Why? I tried to google.

        Like

  6. Hi kalanadis, your simple sample works perfect, thank you! Could you help with some more questions, please?
    1. Which Eclipse do you use? I took the top here https://www.eclipse.org/downloads/ (Eclipse IDE for Java Developers, 154 MB), but when I create a project, I don’t see the Android application (like said in your post).
    2. Which jdk do you use? I read Unity expects 1.6, but it didn’t work either, maybe because I didn’t add any unity jars (like said in your post).
    3. Do you create a separate java file for each class from your post?
    Thanks!

    Like

  7. Hi kalanadis,
    Thank you so much! Finally the plugin works fine! The problem was in my leak java skill (didn’t define a package, some problems with export etc), but finally the base version works well! Now I need some more knowledge in setting alarm, clearing notifications etc to make the local notifications in my game really funny and usefull, so I will dig deeper. Thanks again, you are great!

    Like

  8. Hi kalanadis,
    Sad, but I have a problem again. Cancel methods doesn’t work. I make a notification and want to cancel it if the player starts the game. I call that method cancelAll, but anyway I see the notification in the bar.

    Like

Leave a comment