简体   繁体   English

Unity3D KeyCode.Home问题?

[英]Unity3D KeyCode.Home issue?

Hello i have a line of code that should quit the game upon pressing the three hardware button on the bottom like Menu, Home, Back it seems that the back and the home only is working but the menu is not working 您好,我有一行代码应在按下底部的三个硬件按钮(如“菜单”,“首页”,“后退”)后退出游戏,看来后背和首页仅在起作用,但菜单在起作用

I have a Xiaomi Redmi Note 4X phone, how am i suppose to fix this issue? 我有一台小米Redmi Note 4X手机,我应该如何解决此问题?

Is it the same "Menu" as the other phone where when you press it, it'll show list of the opened app 它与其他手机的“菜单”相同吗?当您按下它时,它会显示已打开的应用程序列表

here's the code: 这是代码:

if (Input.GetKey (KeyCode.Home) || Input.GetKey (KeyCode.Escape) || Input.GetKey (KeyCode.Menu))
    {
        Save ();
        System.Diagnostics.Process.GetCurrentProcess ().Kill ();
    }

Is it the same "Menu" as the other phone where when you press it, it'll show list of the opened ap 它与其他手机的“菜单”相同吗?当您按下该菜单时,它将显示打开的ap的列表

Yes. 是。

I have a Xiaomi Redmi Note 4X phone, how am i suppose to fix this issue? 我有一台小米Redmi Note 4X手机,我应该如何解决此问题?

It should work otherwise it is a bug and you should file for a bug request. 它应该可以工作,否则它是一个错误,您应该提交一个错误请求。 Before you file for abug request, use the script below to make that the Menu key is not mapped to another key. 在提交Abug请求之前,请使用以下脚本确保菜单键未映射到另一个键。

public class KeyCodeFinder : MonoBehaviour
{
    public Text text;

    Array allKeyCodes;

    void Start()
    {
        allKeyCodes = System.Enum.GetValues(typeof(KeyCode));
    }

    void Update()
    {
        foreach (KeyCode tempKey in allKeyCodes)
        {
            if (Input.GetKeyDown(tempKey))
            {
                text.text = "Pressed: KeyCode." + tempKey;
                Debug.Log("Pressed: KeyCode." + tempKey);
            }
        }
    }
}

Meanwhile, you can make a Java plugin that calls your C# function when the Menu keycode is pressed. 同时,您可以制作一个Java插件,该插件在按下菜单键代码时调用C#函数。 That's another option you have. 那是您的另一种选择。

Those buttons are OS related, you don't really control what happens with those. 这些按钮与操作系统有关,您实际上无法控制这些按钮的操作。 Well, you can track them and kill your app but this is going against what every single application does. 好了,您可以跟踪它们并杀死您的应用程序,但这违背了每个应用程序的功能。 That is, if you press the back or home or quit, it gets back to the main OS page, but your app is still on the stack ready to get back. 也就是说,如果您按下后退键或主页键或退出键,它将返回到主OS页面,但是您的应用仍在堆栈中,随时可以返回。

If you want to save when the user quits, maybe you want to look into 如果您想在用户退出时进行保存,也许您想调查一下

https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationPause.html https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnApplicationPause.html

This will make your app behave in a more common way. 这将使您的应用以更常见的方式运行。

On a side note, I would even assume this kind of attempt to over control the app would be rejected for AppStore (iOS) as it would not comply with their generic requirements. 附带一提,我什至会假设这种过度控制应用程序的尝试将被AppStore(iOS)拒绝,因为它不符合其通用要求。

It is allways possible to map a custom KeyCode that is not mapped so far. 始终可以映射到目前为止尚未映射的自定义KeyCode。

First find out what keycode the key has 首先找出钥匙有什么钥匙编码

Event e = Event.current; 
if (e.isKey) 
{ 
    Debug.Log("Detected key code: " + e.keyCode); 
}

Let's say this gives you 10 as keycode (a one that is not mapped in Unities KeyCode so far) 假设这为您提供了10作为键码(到目前为止尚未在Unities KeyCode映射的键KeyCode

You can than simply define a new one like 您可以简单地定义一个新的

KeyCode MY_HOME_BUTTON = (KeyCode) 10;

and than use it like 而不是像

if(Input.MY_HOME_BUTTON)
{
    ...
}

If it is completely possible to catch the home button depends on the OS (most of the time those buttons cannot be catched by an app but are handled by the OS first) 如果完全有可能捕获到主页按钮,则取决于操作系统(大多数情况下,这些按钮无法被应用捕获,而是由操作系统首先处理)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM