无法从统一传回事件的Andr​​oid库JAR事件、Andr、JAR、oid

2023-09-05 11:00:38 作者:思钱想厚

我很新的团结,我想创建一个统一的插件一个Android库JAR,我现在面临以下几个问题:

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:

我不能找到一种方式来传递后退按钮事件的Andr​​oid库。似乎统一prevents该事件被传递到库层。我设法做的是通过设置下面一行通过触摸事件对Android端的活动

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" />

&LT;活动机器人:名称=com.unity3d.player.UnityPlayerProxyActivity...&GT; 在AndroidManifest

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

不过,我无法通过后退按钮事件,我没有改变code库的方式

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

或本

myActivity.onBackPressed() 

而是通过一个C#脚本。我怎样才能实现这个目标在脚本中?

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

推荐答案

C#脚本:

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

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

    }

和你的Andr​​oid的lib

And your Android lib

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

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

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

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

编辑:

如果你想调用onBack pressed,你可以这样做:

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

在YourActivity.java

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

和在C#脚本:

    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");
            }

    }

或内部YourActivity,您可以:

Or inside YourActivity you can:

创建YourActivity扩展UnityPlayerNativeActivty

Create your YourActivity Extends UnityPlayerNativeActivty

包括你的JAR lib和打电话给你想要的功能。

Include your Jar lib and call the function you want.

使用C#脚本调用你的活动。

Use C# script to call your Activity.

请注意,在团结,只有1活动,而且它必须是UnityPlayerNativeActivity或者必须是一个活动的范围是从UnityPlayerNativeActivity。不能使用从瓶子未经UnityPlayerNativeActivity扩展任何其他活动。

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.

如果里面的JAR YourActivity类扩展UnityPlayerNativeActivity。而你不想改变你的JAR文件,然后创建一个新的活动类扩展YourActivity。创建一个新的瓶子+旧罐,并作出新的插件。

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.

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

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

如果您想直接从C#Java类调用一个函数,你仍然可以使用Android NDK构建JNI的lib做

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

此示例演示了如何的Java code可用于互动   Android操作系统以及C ++创建C#和Java之间的桥梁。该   现场包装显示点击它时,按钮获取的   应用程序缓存目录,由Android操作系统的定义。请注意   你将同时需要JDK和Android NDK编译   插件。

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.

在这里参考:http://docs.unity3d.com/Documentation/Manual/PluginsForAndroid.html

下面示例:http://docs.unity3d.com/Documentation/Images/manual/AndroidJavaPlugin.zip

但我不知道你是否可以使用自己的活动或没有。我建议创建一个新的活动从UnityPlayerNativeActivty扩展,因为这种方式比这种方式更简单,unstandable。

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,

弗兰克