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

后端 未结 2 456
滥情空心
滥情空心 2020-12-29 22:08

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

相关标签:
2条回答
  • 2020-12-29 22:40

    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.

    0 讨论(0)
  • 2020-12-29 22:49

    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());
    
    0 讨论(0)
提交回复
热议问题