You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity导航高亮与选中对象不一致,E键触发按钮失效问题排查

Unity UI导航异常:高亮按钮与功能触发对象不匹配

问题现象

使用Unity标准Button组件的自动导航功能,配合自定义脚本实现按E键触发当前选中对象的逻辑,多数菜单正常,但某一菜单出现以下异常:

  • 手动点击任意按钮,功能执行正常
  • 通过W/S键导航切换按钮时,按钮高亮显示符合预期,但按E键无响应
  • 系统判定选中的始终是第一个按钮,切回第一个按钮后按E键功能正常

相关代码

初始E键触发代码(其他菜单正常运行)

// Update is called once per frame
void Update()
{
    if(EventSystem.current.currentSelectedGameObject == this.gameObject && Input.GetKeyDown(_key)){
        //print("button pressed by E");
        _button.onClick.Invoke();
    }
}

设置初始选中对象的代码(多处正常使用,已排除重复调用问题)

private IEnumerator SelectFirstChoice(int thisChoice)
{
    //print("hello from line 177");
    EventSystem.current.SetSelectedGameObject(null);
    yield return new WaitForEndOfFrame();
    EventSystem.current.SetSelectedGameObject(checkpointButtons[thisChoice].gameObject);
}

尝试的修改方案

将触发逻辑改为通过OnSelect事件记录高亮对象,代码如下:

public void OnSelect(BaseEventData eventData)
{
    highlightedChoice = this.gameObject;
}

// Update is called once per frame
void Update()
{
    if(highlightedChoice == this.gameObject && Input.GetKeyDown(_key)){
        //print("button pressed by E");
        _button.onClick.Invoke();
    }
}

修改后问题依旧:即使底部按钮高亮显示,按E键仍只会触发第一个按钮的功能。

按钮设置

使用Unity标准Button组件,导航模式设为自动(Auto Navigation),交互状态配置正常。

更新信息

通过日志验证,EventSystem.current.currentSelectedGameObject确实会随W/S键切换到对应的按钮对象,但按E键时,只有第一个按钮的Update条件会被满足并触发功能。

疑问

为何导航高亮的按钮与系统实际判定的选中对象视觉一致,但功能触发始终指向第一个按钮?

内容的提问来源于stack exchange,提问作者Joseph Squires

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 07:37:23