Canvas中dx类返回按钮异常:点击跳转错误如何修复?
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
- 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.
- Switched to
GetKeyDown: ReplacedInput.GetKeywithInput.GetKeyDown—this ensures the navigation only triggers once per key press, instead of spamming every frame the key is held. - Cleaned Up Code: Created a
SwitchScreenshelper method to avoid repeating the same screen activation/deactivation code across conditions. - 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

