问题
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:
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:
Create your YourActivity Extends UnityPlayerNativeActivty
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
来源:https://stackoverflow.com/questions/21206615/cant-pass-back-event-from-unity-to-android-library-jar