Game Development Stack Exchange is a question and answer site for professional and independent game developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

First, let me say that this is my first experience with Unity, so the answer may be right under my nose. I've also posted this question on Unity's answers site, but plugin questions don't seem to be as frequently answered there.

I'm trying to create a plugin that allows me to access an SDK from my game. I can call SDK methods just fine using AndroidJavaObject and I can pass data to them with no issue. But there are some SDK methods that require an interface to be passed.

For example, my Java function:

public void attemptLogin(String username, String password, LoginListener listener);

Where listener; is a callback interface. I would normally run this code from Java as such:

attemptLogin("username", "password", new LoginListener() {
    public void onSuccess() {
        //Yay! do some stuff in the game

    public void onFailure(int error) {
        //Uh oh, find out what happened based on error

Is there a way to pass a C# interface through JNI/Unity to my attemptLogin function? Or is there a way to create a mimic-ing interface in C# that I can call from inside the Java code (and pass in any kind of parameter)?

Thanks in advance! :)

share|improve this question

Main Unity selling point is that it is cross platform. What this means for your question is that I would strongly suggest not calling SDK functions directly, but doing as much as possible through Unity provided API's. In short do the authentication using Unity API. This way, you will write connectivity code once, and it will work on all platforms. Unity will take care of the platform differences. In particular look at:

If you really need to interoperate C# and Java, I am sorry but my answer is lacking on this.

share|improve this answer
This is true, but in this case I am writing the plugin, not the game ;) – Nick Badal Oct 3 '12 at 0:37
Ah, sorry. Did not get that from the question. Good luck. – MikiJ Oct 3 '12 at 0:58

Since Unity 4.2 there is a class called Androidjavaproxy which can do exactly what you are looking for.

This class can implements any Java interface in Unity.

share|improve this answer

I was confused by your use of the term "interface", since that is a keyword that C# responds to. However, when you described a "callback interface" I realized you're talking about something different: a way for the Java code to call functions in Unity (rather than only the other way around).

This is done using the command UnityPlayer.UnitySendMessage (imported into Java by including Unity's classes.jar file). This command will invoke methods on a named object. So for example, UnityPlayer.UnitySendMessage("GameObjectName", "MethodName", "Message to send") would call MethodName() on the object GameObjectName, and pass the message to that method.

To ensure that my custom plugins have an object to send messages to, I usually have my plugin scripts create an object named something like MyPlugin_instance and use DontDestroyOnLoad() to make sure that object is never removed from the scene. That code looks like:

public class TestPlugin : MonoBehaviour {
   private static TestPlugin _instance;
   public static void Initialize() {
      if (_instance != null) {
         Debug.Log("TestPlugin instance was found. Already initialized");
      Debug.Log("TestPlugin instance not found. Initializing...");
      GameObject owner = new GameObject("TestPlugin_instance");
      _instance = owner.AddComponent<TestPlugin>();
rest of class...
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.