6

I am quite new at Unity and I am trying to create a Unity plugin for an Android library jar and I am facing the following issues:

I can't find a way to pass the back button event to Android library. It seems that Unity prevents this event to be passed to the library layer. What I have managed to do is to pass touch events to the activity on the Android side by setting the following line

<meta-data android:name="unityplayer.ForwardNativeEventsToDal vik" android:value="true" />

at <activity android:name="com.unity3d.player.UnityPlayerProxyActivity" ... > in the AndroidManifest

However I cannot pass the back button event in a way that I do not have to change the code in the library

What I do for now in my script is

public void Update() {

if(Input.GetKeyDown(KeyCode.Escape))
Application.Quit();
} 

However I need the option to pass that back event that I capture, to the library and not handle it at the Unity layer.

Maybe somehow like this.

myActivity.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK)); 

or this

myActivity.onBackPressed() 

but through a C# script. How can I accomplish that within a script?

1 Answer 1

8
+100

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

2
  • thanks for the response but what I have is a jar that takes as reference the activity and handles it accordingly. The way you described works only if I change the jar file. Isn't there anyway to pass the back key event to the activity layer through the C# script.
    – andreasv
    Jan 20, 2014 at 13:32
  • Ok, see my updated answer. Hope that it can help to solve your problems. Jan 23, 2014 at 4:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.