Callback Listener in Unity - How to call script file method from UnityPlayerActivity in Android Callback Listener in Unity - How to call script file method from UnityPlayerActivity in Android android android

Callback Listener in Unity - How to call script file method from UnityPlayerActivity in Android


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());


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.