如何在Android中使用关键事件。我已将我的游戏导出到" Google Android项目"我已经尝试重写dispatchKeyEvent()和onGenericMotionEvent(),如下所示:
// For some reason the multiple keyevent type is not supported by the ndk.
// Force event injection by overriding dispatchKeyEvent().
@Override public boolean dispatchKeyEvent(KeyEvent event)
{
Log.d(String.valueOf(event.getDeviceId()), "Control");
Toast.makeText(this, String.valueOf(event.getDeviceId()), Toast.LENGTH_SHORT).show();
if (event.getAction() == KeyEvent.ACTION_MULTIPLE)
return mUnityPlayer.injectEvent(event);
return super.dispatchKeyEvent(event);
}
@Override
public boolean onGenericMotionEvent(MotionEvent event)
{
Log.d(String.valueOf(event.getDeviceId()), "Control");
Toast.makeText(this, String.valueOf(event.getDeviceId()), Toast.LENGTH_SHORT).show();
return true;
}
我还尝试将KeyListener处理程序直接附加到UnityPlayer对象,如下所示:
mUnityPlayer = new UnityPlayer(this);
mUnityPlayer.setOnKeyListener(new View.OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
Log.d(String.valueOf(event.getDeviceId()), "Control");
Toast.makeText(UnityPlayerNativeActivity.this, String.valueOf(event.getDeviceId()), Toast.LENGTH_SHORT).show();
return true;
}
}
);
但在这两种情况下,Java代码都没有触发。任何想法为什么??
答案 0 :(得分:0)
所以我可以通过在清单中的Unity活动下面添加以下内容来获取Jostick AXIS的值。现在,这会触发onGenericMotionEvent(),但不会触发dispatchKeyEvent()。
<meta-data
android:name="unityplayer.ForwardNativeEventsToDalvik"
android:value="true" />
我假设这是一个错误(或''的'特征'),所以会直接向他们提交错误报告