How to use buttons in Unity with and without a mouse

Roger moore
3 min readAug 8, 2023

--

Objective: go over making a menu selection with and without a mouse

Buttons are the backbone of user interaction in Unity projects, enabling players to navigate menus and trigger in-game actions. However, ensuring a smooth user experience across different input methods, including mouse and keyboard, is crucial. In this tutorial, I’ll walk you through the process of creating versatile menus that can be effortlessly navigated both with and without a mouse. Join me as we explore the creation of interactive menus that adapt to diverse player preferences.

Step 1: Setting Up the Canvas

  1. Creating Dual Panels: To achieve a menu system that seamlessly accommodates both mouse and keyboard inputs, I began by setting up two separate panels within a Canvas. Each panel is associated with a Canvas Group component, allowing us to control their visibility and interaction individually.
  2. Custom Button Prefab: To streamline the process and maintain visual consistency, I designed a custom button prefab. This prefab includes animation transitions for various button states: normal, highlighted, pressed, and selected. This ensures a visually appealing and responsive user interface.

Scripting the Menu Navigation

  1. Introducing ButtonScript: The core of our adaptable menu lies within the ButtonScript. This script handles menu navigation and interaction between the two panels.
  2. Canvas Group Interaction: In the ButtonScript’s Start() method, I initialized the menu by activating the default panel while deactivating the other. This is achieved by adjusting the Canvas Group’s interactable, blocksRaycasts, and alpha properties.
  3. Switching Menus: The SwitchMenu() method facilitates menu swapping. It disables the current menu, sets the index to the new menu, and enables its interactivity. This ensures a seamless transition between menus regardless of the input method.
  4. Focus Management: To enhance user experience, the SwitchMenu() method ensures that the first button in the newly activated menu gains focus. This makes navigating the menu with the keyboard intuitive and user-friendly.

Button Setup and Functionality

  1. Configuring Button Interactivity: For each button, I configured the onClick event to call the SwitchMenu() method, redirecting to the desired menu. This establishes a straightforward method for users to switch between menus with a single click.
  2. Dynamic Menu Interaction: The combination of Canvas Groups and ButtonScript results in dynamic menu interaction. Whether users employ a mouse or keyboard, the menus respond fluidly to their actions.

Testing and Fine-Tuning

  1. Playtesting the Menus: I rigorously playtested the menus, ensuring that both mouse and keyboard inputs yielded consistent and intuitive results. This involved switching between menus, hovering over buttons, and selecting options.
  2. Refining the User Experience: If any discrepancies or issues were identified during playtesting, I revisited the script to make necessary refinements. This iterative process is essential to guarantee a seamless user experience.

By following this comprehensive guide, you’ve mastered the art of creating adaptive menus in Unity. You’ve learned how to set up dual panels within a Canvas, design a custom button prefab, and script menu navigation for diverse input methods. Your project now boasts menus that seamlessly transition between mouse and keyboard interactions, enhancing user engagement and accessibility. As you continue to develop your Unity skills, consider applying these principles to other aspects of your game, ensuring a consistent and user-centric experience across the entire gameplay journey. The ability to create versatile and accommodating interfaces showcases your expertise in game design and sets you on the path to crafting unforgettable player experiences.

--

--