I’m new to Unity and Unity UI (not using UI Toolkit, just the default Unity UI in Unity 6), and I’m working on a 100% UI-driven game (a text-based game with a modern UI presentation). Because the game relies entirely on UI for navigation and interaction, solving this issue is critical to ensuring a smooth and intuitive user experience. Here’s what I have and what I’m trying to solve:
Setup:
Navigation Methods:
Gamepad: Left analog stick or D-Pad for navigation. [functional]
Keyboard: Arrow keys for navigation. [functional]
Mouse: Hovering over a button automatically selects it. [functional]
Popup Menus (e.g., Confirmation Menus):
When a popup menu opens, it should remember and highlight the last selected button from the menu that called it.
When the popup closes (either through confirmation or cancel), the last selected button in the calling menu should be restored.
Other Menus:
For non-popup menus, selecting the first active button in the panel on enable works fine, and I have this implemented in the OnEnable method.
Preventing Deselection:
Clicking on empty space (areas without buttons) should never clear the selected button in Unity’s EventSystem. The last selected button should remain active even after a mouse click on empty space.
Challenge:
How can I:
Prevent Unity’s EventSystem from deselecting the currently selected button when clicking on empty space?
Properly track and restore the last selected button for popup menus, ensuring smooth transitions between mouse and gamepad/keyboard input?
Final Thoughts:
I’d appreciate any resources, tutorials, or advice on best practices for managing Unity UI with these requirements. Since I’m still new to Unity and Unity UI, examples or references would be especially helpful. Thank you!
For a serious UI game like that, you should really roll your own selection system, and drop the Selectable stuff. It’s very easy to get something that’s more intuitive and well-behaved than the built in one when you build the system for your game rather than every game.
As an example, the code for automatic navigation between buttons makes no assumptions about any hierarchy layout, and tried to do it’s best to handle buttons being placed completely arbitrarilly in 2D space. In almost all real menus, you just want to go vertically or horizontally through the transform hierarchy. That’s easy to write, has less strange bugs, and performs better, because you’re writing selection code for your UI instead of all UIs.
In addition, the built in UI (Canvas) has been on life support with almost no new features since it shipped for Unity 4.6. The Unity team has repeatedly said that it will only get bug fixes, and all new UI effort is going towards UI Toolkit.
I got this working in my current project (which uses UI Toolkit) by creating an ‘overlay’ system that keeps track of each overlay opened in a stack. When an overlay is opened, it gets wrapped up in an class that does some internal management - such as tracking the last focused Visual Element - and added to the stack. When an overlay is closed, the next overlay on the stack refocuses the last focused visual element. This is on top of a bunch of other internal management that automates a lot of my game’s UI.
I just need to ensure all overlays are opened via said system.
No doubt a similar system can be implement for uGUI.
I see what you mean. UI Toolkit does seem like a better focus, and I like how it separates the UI from the game world space—it feels cleaner that way. I didn’t find much information on gamepad support for it, but it seems relatively new, so that’s probably why.
I watched a tutorial where the selection system accounted for the x and y direction of the arrow keys, d-pad, or analog stick. It cycled through an array of buttons to select the next one in line, and the menu even remembered the last selection for a seamless experience. I might revisit that tutorial and go fully custom since my game’s input needs are pretty simple. I didn’t anticipate how much of a time sink UI/UX would be for me, but it always seems to end up that way. lol
This approach is intuitive and sounds really effective. I haven’t worked with UI Toolkit yet, but I’ve watched a few videos on it, and it seems awesome. If I were making a mouse-only game, I’d probably have switched to it right away because of how convenient the UI/World separation is. Since it’s the future of Unity’s UI integration, I figure I might as well dive in.
I’ve been reading through some Unity e-books on different topics—would you say those are enough to grasp UI Toolkit, or are there other resources you’d recommend?
In the meantime, I might try implementing this with my GameUIManager in uGUI since I (kinda, sorta, mostly) know how that system works. lol
I mostly learnt by doing, which is how I came to my current overarching UI Toolkit workflow.
At the very least, play with it in isolation. Do some tutorials to play with the basics. Try do something with it your project requires. That will be more informative.