首页 > 解决方案 > 不明白为什么它无法识别关键输入

问题描述

当我按下退出键时,我的游戏中的暂停菜单应该对用户可见,并且游戏时间应该冻结。但是,当我按 Escape 时,程序似乎无法识别输入。我尝试过使用不同的密钥,但它们也不起作用。我通过执行 Debug.Log 命令来确保输入是问题所在,当我进行测试时,我仍然没有得到任何触发的迹象。这是代码。我希望有人可以帮助我。

public static bool GameIsPaused = true;

public GameObject PauseMenuUI;
// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.Escape))
    {
        if (GameIsPaused)
        {
            Resume();
        }
        else
        {
            Pause();
        }
    }
}

void Resume()
{
    PauseMenuUI.SetActive(false);
    Time.timeScale = 1f;
    GameIsPaused = false;
}

void Pause()
{
    PauseMenuUI.SetActive(true);
    Time.timeScale = 0f;
    GameIsPaused = true; 
}

标签: c#unity3dinput

解决方案


正如我们在评论中所讨论的,PauseMenu-script 附加到的 GameObject 未激活。

不活动的游戏对象将禁用其所有组件。

如果启用了 MonoBehaviour,则每帧都会调用更新。 https://docs.unity3d.com/ScriptReference/MonoBehaviour.Update.html

所以你的更新代码没有运行,因此永远不会检测到何时按下 Escape。


推荐阅读