[DEV] Menu Controller Support

Since our Game needs to have Keyboard/Mouse and Controller support, creating menus with interactable UI elements isn’t as easy as usual. Clicking on a Button with your mouse is easy, but Controllers don’t have a mouse, nor are they able to control the Cursor. To fix this issue, I’ve decided to get rid of the Cursor entirely (Now always locked and invisible in the game), and instead select the UI elements using a Script.

Going through the different elements wasn’t as difficult as anticipated. Unity’s UI Elements inherit from a Selectable class, where I can get any adjacent UI element. It is then very easy to get the Input from your controls and look for the next element using that method. Controls are currently set as WSAD or Arrow Keys for Keyboard, and D-PAD for Controller. The submit key is X or Enter for Keyboard, and X for Controller. To change the values of a Slider, just simply have it selected (highlighted) and move left or right.

One issue that emerged from using these controls, is that Unity internally already does this selecting based on input. Since their axis is the same as ours, holding down any of these keys will make the highlight blink on the wrong button, or highlight a wrong button until you let go of the key. To prevent this, I had to manually overwrite the Colors depending on the selected element, instead of relying on the normal and highlighted colors that are defined the Button.

This also means I have to keep track of the last used button, so I can reset its colors when another button is selected. This gave me the opportunity to add a system that remembers which button you last pressed, which is convenient when switching from Main Menu to Settings Menu and back, for example.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s