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

Canvas中dx类返回按钮异常:点击跳转错误如何修复?

Fixing Escape Button & dx Class Navigation Issues

Let's break down your problem and fix it step by step:

Root Cause

From your code snippet, it looks like your dx class's Update method only handles the Escape key logic for the Login screen, but there's no dedicated handling for the Bought screen. This means when you press Escape in Bought, it's falling through to some unshown condition that triggers a jump to MainMenu instead of MainMarket. Also, using Input.GetKey can cause unintended repeated triggers since it fires every frame the key is held down.

Solution Code

Here's the corrected dx class with proper navigation logic for all relevant screens:

public class dx : MonoBehaviour 
{ 
    public GameObject MainMenu; 
    public GameObject Login; 
    public GameObject MainMarket; 
    public GameObject Bought; 

    void Update() 
    { 
        // Handle Bought screen back navigation first (matches your requirement)
        if (Bought.activeSelf && Input.GetKeyDown(KeyCode.Escape)) 
        { 
            SwitchScreens(Bought, MainMarket);
            Debug.Log("Returned to MainMarket from Bought"); 
            return; 
        }

        // Handle Login screen back navigation
        else if (Login.activeSelf && Input.GetKeyDown(KeyCode.Escape)) 
        { 
            SwitchScreens(Login, MainMenu);
            Debug.Log("Returned to MainMenu from Login"); 
            return; 
        }

        // Optional: Add MainMarket back navigation to MainMenu
        else if (MainMarket.activeSelf && Input.GetKeyDown(KeyCode.Escape))
        {
            SwitchScreens(MainMarket, MainMenu);
            Debug.Log("Returned to MainMenu from MainMarket");
            return;
        }
    }

    // Helper method to clean up screen switching code
    private void SwitchScreens(GameObject currentScreen, GameObject targetScreen)
    {
        currentScreen.SetActive(false);
        targetScreen.SetActive(true);
    }
}

Key Fixes Explained

  1. Added Bought Screen Logic: We added a top-priority condition to check if the Bought screen is active. When Escape is pressed, it hides Bought and shows MainMarket exactly as you expected.
  2. Switched to GetKeyDown: Replaced Input.GetKey with Input.GetKeyDown—this ensures the navigation only triggers once per key press, instead of spamming every frame the key is held.
  3. Cleaned Up Code: Created a SwitchScreens helper method to avoid repeating the same screen activation/deactivation code across conditions.
  4. Ordered Conditions: By placing the Bought screen check first, we ensure it doesn't get overridden by lower-priority conditions.

If You're Using a UI Back Button

If your "return button" is a UI element (not just the Escape key), add this method to the dx class and bind it to your button's OnClick event in the Unity Editor:

public void OnBackButtonClicked()
{
    if (Bought.activeSelf)
    {
        SwitchScreens(Bought, MainMarket);
        Debug.Log("Returned to MainMarket from Bought via UI button");
    }
    else if (Login.activeSelf)
    {
        SwitchScreens(Login, MainMenu);
        Debug.Log("Returned to MainMenu from Login via UI button");
    }
    else if (MainMarket.activeSelf)
    {
        SwitchScreens(MainMarket, MainMenu);
        Debug.Log("Returned to MainMenu from MainMarket via UI button");
    }
}

Just drag your dx component onto the button's OnClick field and select this method from the dropdown.

内容的提问来源于stack exchange,提问作者ned.juliyan

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.25 08:31:55