How to Create Android Java Callbacks to C# in Unity

This tutorial will go through a proper way to bridge method calls between Unity and Android plugins. We will use AndroidJavaProxy to implement a Java interface in a C# class. AndroidJavaProxy allows us to call methods in Java that will invoke the matching method on the C# class. Alternative way is to use SendMessage to invoke C# methods from Java but you would have to depend on GameObject names and method names inside the Java code which is suboptimal.

First we create a new Android plugin project in Android Studio. This tutorial will not go into detail about creating Android plugins for Unity so if you are not familiar with the process here is a tutorial that explains it: How to create an Android Java Plugin

Now we create a Java interface inside our plugin that our AndroidJavaProxy C# class can implement. It is a regular interface in which we can create as many callbacks we may need. For this tutorial we create just one:

package com.vuopaja.androidjavaproxy;

public interface JavaCallback
{
     void OnJavaCallback(int index);
}

Next we need a Java class for our plugin functionality. In the constructor it gets a reference to a C# class that implements our interface. Here is a simple example class that passes a random integer from Java to C# with a callback instead of returning it from the method call. The real benefit of using callbacks like this is when we need to pass something asynchronously from Java to C# but for the sake of simplicity we just pass a random number here:

package com.vuopaja.androidjavaproxy;
import java.util.Random;

public class JavaClass
{
     public JavaCallback Callback;
    
     // The constructor takes a reference to the C# class
     // that implements the JavaCallback interface
     public JavaClass(JavaCallback callback)
     {
         Callback = callback;
     }
    
     public void JavaMethodName()
     {
         Random random = new Random();
         Callback.OnJavaCallback(random.nextInt(10));
     }
}

That is all the code we need in the plugin so next we can build the plugin and import it into our Unity project.

Now we need to create a C# class that implements the JavaCallback interface with AndroidJavaProxy. Here is an example class that inherits the AndroidJavaProxy and implements the Java interface we created. We declare the class to inherit from AndroidJavaProxy and in the constructor set the base class to reference the Java interface in our plugin. We also create a new instance of our Java class and store the reference in an AndroidJavaObject variable. The constructor of the Java class we created takes an object that implements the JavaCallback as a parameter so we pass our C# class to it. With that we have linked our Java and C# classes so that when we call the Java method in our plugin the OnJavaCallback method gets invoked in C#.

using UnityEngine;
using System;

public class JavaBridge : AndroidJavaProxy
{
     public Action GotJavaCallback;
    
     AndroidJavaObject javaObject;
    
     public JavaBridge() : base("com.vuopaja.androidjavaproxy.JavaCallback")
     {
         // We create an instance of the JavaClass in the constructor
         // and pass the reference of this class to the JavaClass
         javaObject = new AndroidJavaObject("com.vuopaja.androidjavaproxy.JavaClass", this);
     }
    
     // Call the method in the plugin to invoke the callback
     public void CallJavaMethod()
     {
         javaObject.Call("JavaMethodName");
     }
    
     // This method will be invoked from the plugin
     public void OnJavaCallback(int index)
     {
         // Pass the result to the C# event that we register to in the UI class
         if (GotJavaCallback != null) GotJavaCallback(index);
     }
}

We also create a simple C# event to then further pass the result from our plugin to the rest of the Unity application. The example GitHub repository shows also how this could then be linked to show the result in an UI element. But as far as the actual Java / C# communication is conserned that is it.

Thanks for reading! If you have any questions click here to send us email.