Can't pass back event from Unity to android library jar

时光总嘲笑我的痴心妄想 提交于 2019-11-29 02:29:28

C# script:

        void Update ()
    {
            if (Input.GetKeyUp (KeyCode.Escape)) {

                    AndroidJavaClass jc = new AndroidJavaClass ("com.unity3d.player.UnityPlayer"); 
                    AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject> ("currentActivity"); 
                    jo.Call ("yourBackEventFunction");
            }

    }

And your Android lib

    public class YourActivity extends
        com.unity3d.player.UnityPlayerNativeActivity {
…

    public void yourBackEventFunction() {
        // Do something here
    }
…
    }

========================

Edit:

If you want to call onBackPressed, you can do so:

In YourActivity.java

@Override
public void onBackPressed() {
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(UnityPlayerNativeActivity.this,
                    "on Back Pressed", Toast.LENGTH_SHORT)
                    .show();
        }
    });
    super.onBackPressed();
}

And in C# script:

    void Update ()
    {
            if (Input.GetKeyUp (KeyCode.Escape)) {
                    Debug.Log ("onResume Received");
                    AndroidJavaClass jc = new AndroidJavaClass ("com.unity3d.player.UnityPlayer"); 
                    AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject> ("currentActivity"); 
                    jo.Call ("onBackPressed");
            }

    }

Or inside YourActivity you can:

  1. Create your YourActivity Extends UnityPlayerNativeActivty

  2. Include your Jar lib and call the function you want.

Use C# script to call your Activity.

Note that in Unity, there is only 1 Activity, and it must be UnityPlayerNativeActivity OR it must be an Activity EXTENDS FROM UnityPlayerNativeActivity. You can not use any others activity from your Jar without extend from UnityPlayerNativeActivity.

If YourActivity class inside JAR extends UnityPlayerNativeActivity. and you don't want to change your JAR, then you create a new Activity class extends YourActivity. Create a new Jar + old Jar and make a new plugins.

========================

If you want to call a function directly from C# to Java class, you still can do it by using Android NDK to build an JNI lib

This sample demonstrates how Java code can be used to interact with the Android OS and how C++ creates a bridge between C# and Java. The scene in the package displays a button which when clicked fetches the application cache directory, as defined by the Android OS. Please note that you will need both the JDK and the Android NDK to compile the plugins.

Reference here: http://docs.unity3d.com/Documentation/Manual/PluginsForAndroid.html

Example here: http://docs.unity3d.com/Documentation/Images/manual/AndroidJavaPlugin.zip

But I'm not sure if you can use your own Activity or not. And I recommend creating a new Activity Extends from UnityPlayerNativeActivty, because that way is more simple and unstandable than this way.

Br,

Frank

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!