Calling C# code from Java and Kotlin plug-in code
Call C# script code from your Java and Kotlin plug-in code.
Read time 4 minutesLast updated 9 days ago
Java and Kotlin plug-in code can call your C# scripts, for example, to notify Unity of an Android event or to return a result from your plug-in code. There are two ways to do this:
- Send a message to a GameObject with .
UnitySendMessage - Call a C# method through a callback interface with AndroidJavaProxy.
For the equivalent C# to Java and Kotlin implementation, refer to Calling Java and Kotlin plug-in code from C# scripts.
Send a message to a GameObject
Use to send a string from Java or Kotlin plug-in code to a method on a named GameObject. This is a one-way call where the C# method receives the string but doesn't return a value to the plug-in. Use it for simple notifications.
UnitySendMessageThe plug-in calls with three arguments: the name of the target GameObject, the name of the method to call, and the string message.
UnitySendMessageThe following Java example implements :
UnitySendMessagepackage com.example.androidplugin;import com.unity3d.player.UnityPlayer;public class MessageSender { public static void sendToUnity(String gameObjectName) { UnityPlayer.UnitySendMessage(gameObjectName, "OnMessageReceived", "Hello from Java"); }}
The following Kotlin example implements :
UnitySendMessagepackage com.example.androidpluginimport com.unity3d.player.UnityPlayerobject MessageSender { @JvmStatic fun sendToUnity(gameObjectName: String) { UnityPlayer.UnitySendMessage(gameObjectName, "OnMessageReceived", "Hello from Kotlin") }}
In your C# script, add the method that calls. Ensure the script is attached to a GameObject whose name matches the first argument.
UnitySendMessageusing UnityEngine;public class MessageReceiver : MonoBehaviour{ void Start() { gameObject.name = "MessageReceiver"; using var sender = new AndroidJavaClass("com.example.androidplugin.MessageSender"); sender.CallStatic("sendToUnity", gameObject.name); } void OnMessageReceived(string message) { Debug.Log("Message from plug-in: " + message); }}
This example:
- Sets the GameObject's name to match the target name the plug-in sends.
- Calls the plug-in, which uses to send a string back to the
UnitySendMessagemethod on the named GameObject.OnMessageReceived - Logs the message that the plug-in sends.
UnitySendMessage- You can only call C# methods with the signature .
void MethodName(string message) - Calls are asynchronous and have a delay of one frame.
- Multiple GameObjects with the same name can cause conflicts.
To send data other than a string, serialize it to a JSON string. For more information, refer to Supported data types for Java/Kotlin and C# code.
Call a C# method through a callback interface
Use AndroidJavaProxy to implement a Java or Kotlin interface in C#. When the plug-in calls a method on the interface, Unity runs your C# implementation. Use this approach for typed callbacks, or to pass structured data or return values.
Define an interface in the plug-in and a class that calls it. The following plug-in code calls the interface method to pass a message back to C#.
Java plug-in code
package com.example.androidplugin;public class Greeter { public interface GreetingCallback { void onGreeting(String message); } public void requestGreeting(GreetingCallback callback) { callback.onGreeting("Hello from Java"); }}
### Kotlin plug-in code
package com.example.androidpluginclass Greeter { interface GreetingCallback { fun onGreeting(message: String) } fun requestGreeting(callback: GreetingCallback) { callback.onGreeting("Hello from Kotlin") }}
In your C# script, create a class that derives from and implements the interface. Pass an instance of this class to the plug-in.
AndroidJavaProxyusing UnityEngine;public class GreetingExample : MonoBehaviour{ class GreetingCallback : AndroidJavaProxy { public GreetingCallback() : base("com.example.androidplugin.Greeter$GreetingCallback") { } void onGreeting(string message) { Debug.Log("Greeting from plug-in: " + message); } } void Start() { using var greeter = new AndroidJavaObject("com.example.androidplugin.Greeter"); greeter.Call("requestGreeting", new GreetingCallback()); }}
This example:
- Creates a C# class that derives from
GreetingCallbackand passes the interface name to the base constructor. BecauseAndroidJavaProxyis nested in theGreetingCallbackclass, the name uses theGreeterseparator:$.com.example.androidplugin.Greeter$GreetingCallback - Implements with a signature that matches the interface method. Unity automatically routes calls on the interface to this C# implementation.
onGreeting - Creates an for the
AndroidJavaObjectplug-in class and callsGreeter, passing the proxy. The plug-in then callsrequestGreeting, which runs the C# implementation.onGreeting
For more information on writing the plug-in code, refer to Java and Kotlin source plug-ins.