无法将事件从 Unity 传递回 android 库 jar [英] Can't pass back event from Unity to android library jar

查看:21
本文介绍了无法将事件从 Unity 传递回 android 库 jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Unity 的新手,我正在尝试为 Android 库 jar 创建 Unity 插件,但我面临以下问题:

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:

我找不到将后退按钮事件传递给 Android 库的方法.似乎Unity阻止了这个事件被传递到库层.我设法做的是通过设置以下行将触摸事件传递给 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" />

at <activity android:name="com.unity3d.player.UnityPlayerProxyActivity" ... > 在 AndroidManifest 中

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

我现在在脚本中做的是

public void Update() {

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

但是,我需要选择将我捕获的返回事件传递给库,而不是在 Unity 层处理它.

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

也许有点像这样.

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

    }

还有你的安卓库

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

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

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

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

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

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:

  1. 创建您的活动扩展 UnityPlayerNativeActivty

  1. Create your YourActivity Extends UnityPlayerNativeActivty

包括您的 Jar 库并调用您想要的函数.

Include your Jar lib and call the function you want.

使用 C# 脚本调用您的活动.

Use C# script to call your Activity.

请注意,在 Unity 中,只有 1 个 Activity,并且它必须是 UnityPlayerNativeActivity 或者它必须是一个 Activity EXTENDS FROM UnityPlayerNativeActivity.如果不从 UnityPlayerNativeActivity 扩展,您将无法使用 Jar 中的任何其他活动.

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,然后您创建一个新的 Activity 类来扩展 YourActivity.创建一个新 Jar + 旧 Jar 并制作一个新插件.

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库来实现

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 代码与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

但我不确定您是否可以使用自己的 Activity.并且我建议从 UnityPlayerNativeActivty 创建一个新的 Activity Extends,因为这种方式比这种方式更简单也更难理解.

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.

兄弟,

弗兰克

这篇关于无法将事件从 Unity 传递回 android 库 jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆