I have an android library project and imported the library project in the Unity project. Now, I want to implement a callback in Unity project, which will execute according t
I believe you are only allowed to call UnitySendMessage() from the main thread - at least in one scenario above you are calling it from the Android UI worker thread.
As a quick sanity test, try calling it before you right at the top of your shareText() function.
Another option will be to implement an interface callback using AndroidJavaProxy. Instead of using UnitySendMessage, you can simply have an Interface callback in your java code and then implement this interface in C# using AndroidJavaProxy and pass it to the Java method in order to receive messages back.
Create your Java interface:
package com.example.android;
public interface PluginCallback {
public void onSuccess(String videoPath);
public void onError(String errorMessage);
}
Call the passed listener/callback to return messages
public void myPluginMethod(PluginCallback callback) {
// Do something
callback.onSuccess("onSuccess");
// Do something horrible
callback.onError("onError");
}
Implement the interface in C#
class AndroidPluginCallback : AndroidJavaProxy
{
public AndroidPluginCallback() : base("com.example.android.PluginCallback") { }
public void onSuccess(string videoPath) {
Debug.Log("ENTER callback onSuccess: " + videoPath);
}
public void onError(string errorMessage)
{
Debug.Log("ENTER callback onError: " + errorMessage);
}
}
Pass the C# interface to the Java method
AndroidJavaObject pluginClass = new AndroidJavaObject("com.example.android.MyPlugin");
pluginClass.Call("myPluginMethod", new AndroidPluginCallback());