c# - 不明白为什么它无法识别关键输入
问题描述
当我按下退出键时,我的游戏中的暂停菜单应该对用户可见,并且游戏时间应该冻结。但是,当我按 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;
}
解决方案
正如我们在评论中所讨论的,PauseMenu
-script 附加到的 GameObject 未激活。
不活动的游戏对象将禁用其所有组件。
如果启用了 MonoBehaviour,则每帧都会调用更新。 https://docs.unity3d.com/ScriptReference/MonoBehaviour.Update.html
所以你的更新代码没有运行,因此永远不会检测到何时按下 Escape。
推荐阅读
- postgresql - Postgres:一些进程正在清理我所有的表记录
- python - 用 Pandas 读取 Access 表的最简单方法是什么?
- xml - XSD 验证错误:具有目标命名空间的重新定义架构中不存在元素
- c# - MessageLockLostException:提供的锁无效。锁已过期,或者消息已从队列中删除
- typescript - Typescript - 使用 Object.entries 动态更新对象属性
- python-3.x - 如何使用 Dask 从文本文件中加载一个大的 numpy 数组?
- docker - 从 docker 启动 docker - 无法连接到守护进程
- c++ - 为什么 std::ranges 算法缺少并行重载(采用执行策略参数)?
- javascript - 如何从 ngx-datatables 中删除边框?
- django - 未调用 DRF 自定义 check_permission