Controlling player cursor across windowed UI

Hi everyone,

I’m at a loss of how to approach a way to manipulate what sections of a UI interface a player can/can’t access, based on what the cursor has selected.

I have this UI mocked up:

Here are my struggles, I’m hoping someone can help point me in the right direction for figuring this out:

  1. When this menu is opened, I would like for the ‘Equipment’ button to already start highlighted, and for movement up and down from Equipment through Crafting buttons to be handled with the arrow keys or WASD. While I know how to control player movement via scripting, I am unsure how to control something like this.

  2. When Equipment is selected using the Submit button (Enter in my case), the selector should then move to the item buttons shown on the right, allowing movement between with WASD/arrows as before. When an item is highlighted (such as the Rusty Sword), the interface below should update with its description and icon - this part I can probably figure out, since that opens up a whole can of worms on how I’ve coded the inventory objects that would probably need its own other thread. However, it would be nice to understand how UI elements such as that can be updated based on what is currently highlighted.

  3. And finally, if the player hits ESC while in this item area, their cursor moves back to the Equipment - Crafting area.

Hopefully someone can help me understand - I have learned a lot about coding but in this area (coding to control the UI and highlighting), I am at a loss on where to begin. If I have caused any confusion in my explanation please let me know so I can make clarifications. :slight_smile:

Thanks in advance!

Start looking into Unity UI Navigation: Navigation Options | Unity UI | 1.0.0 and the Selectable component: Selectable Base Class | Unity UI | 1.0.0

Of utmost importance also are the InputModule which lets you define which inputs control the navigation, and the Unity EventSystem, which among other things, is the object that keeps track of what is currently “selected” in the UI.

1 Like

Thanks for providing those resources. However, I’m not sure how to actually get the cursor moving. Right now it is only using the mouse to hover over the buttons, when I need it to be controlled by the keyboard. I can’t add ‘Selectable’ to this because it already has a Button component, yet the button component has no options for determining how to select it. Here is my hierarchy if that helps, perhaps I have something wrong here:

Buttons inherit from Selectable, so they are already Selectable. You need a few things for navigation to work:

  • Make sure your EventSystem selects something in your UI to start with. You can set a “First Selected” object in your event system. Alternatively you can select something in OnEnable for your UI panel or wherever appropriate with EventSystem.current.SetSelectedGameObject(…).
  • Make sure your Input Module (StandaloneInputModule or Input System UI Input Module) exists and the axes/inputs for ui navigation are set up properly.
  • Buttons (like all Selectables) have navigation settings in the inspector for the Button component. Set those up to configure navigation directions. Auto usually works fine.
  • Buttons (like all Selectables) have settings for appearance and transitions when being selected/deselected etc… Make sure those are set up.
1 Like

Ah this seemed to be the missing piece for me - I have not worked with the Event System before so that makes sense. Thank you!

This brings me to another question. My inventory items (buttons) are instantiated as the player picks them up, using a prefab. How would it be possible to use the UnityEvent system to use OnClick on the Equipment button to then have the selector move to the first item in the item list, as long as there is one? I’m not sure if the UnityEvent system is able to do this, or if it would require some sort of script.