← Back to all addons

🖱🎯 Mouse Navigation Support

Posted by Gyvr on Jan 15, 2025 at 10:16 PM

GameplayFeature2.0 Verified
🔥 2 ❤️ 2

This add-on will enable mouse support for your game! After following these instructions, all your menus should be usable with a mouse! Simply hover an element to select it, and use a “Left Click” to interact!

  1. Find your PlayerInputActions asset in your project (it should by default be under: Assets/Mythril2D/Demo/Settings/PlayerInputActions.inputactions)
  2. Click on the PlayerInputActions asset => your inspector should now display a button “Edit Asset”.
  3. Hit the “Edit Asset” button
  4. In the newly opened window (Input Actions), click on the “All Control Schemes” dropdown menu, and select “Keyboard”
  5. Click on the “All Control Schemes” dropdown menu again, and this time select “Edit Control Scheme…”
  6. Click the + button, and add “Mouse” as a device type. You should now have 2 device types: <Keyboard>, and <Mouse>. Additionally, you can rename the “Scheme Name” from “Keyboard” to “Keyboard&Mouse”
  7. Save the changes to the control scheme.
  8. Under “Action Maps”, select UI
  9. Right click on the “Submit” action, and select “Add Binding”: a new binding should have been created: <No Binding> {GLOBAL}
  10. Edit the newly created binding, by setting its “Path” to “Mouse > Left Button”
  11. Under the “Use in control scheme” label, select “Keyboard” (or “Keyboard&Mouse” if you renamed the control scheme)
  12. Back to the “Actions” panel, right click anywhere in the blank space under “Skip” to create a new action with the “Add Action” button. Name this new action “Point”
  13. Set the “Point” action “Action Type” to “Pass Through”, and the “Control Type” to “Vector 2”.
  14. Expand the “Point” action with the ▶️ icon to the left of its name
  15. Select the empty binding: <No Binding> {GLOBAL}
  16. Set its “Path” to “Mouse > Position”
  17. Check the “Keyboard” (or “Keyboard&Mouse”) checkbox under the “Use in control scheme” label
  18. Hit the “Save Asset” button at the top right of the current window.
  19. Open the “Main Menu” scene (Assets/Mythril2D/Demo/Scenes/Main Menu.unity)
  20. Type t:InputSystemUIInputModule in the hierarchy search bar.
  21. Select the GameObject that shows up, and find its “Input System UI Input Module” component in the inspector.
  22. Set the “Point” action to use “UI/Point”
  23. Save the scene
  24. Open the “Gameplay” scene (Assets/Mythril2D/Demo/Scenes/Gameplay.unity) and repeat steps 20 to 23
  25. Open the InputSystem.cs script (Assets/Mythril2D/Core/Runtime/Scripts/Game/Systems/InputSystem.cs), and add this function anywhere inside of the InputSystem class:
private void Update()
{            
    InputAction pointAction = playerInput.actions.FindActionMap("UI").FindAction("Point");

    if (pointAction.ReadValue<Vector2>() != Vector2.zero)
    {
        var eventSystem = UnityEngine.EventSystems.EventSystem.current;

        if (eventSystem.IsPointerOverGameObject())
        {
            var pointerEventData = new UnityEngine.EventSystems.PointerEventData(eventSystem)
            {
                position = Mouse.current.position.ReadValue()
            };

            var results = new System.Collections.Generic.List<UnityEngine.EventSystems.RaycastResult>();
            eventSystem.RaycastAll(pointerEventData, results);

            foreach (var result in results)
            {
                var selectable = result.gameObject.GetComponentInParent<UnityEngine.UI.Selectable>();
                if (selectable != null)
                {
                    eventSystem.SetSelectedGameObject(selectable.gameObject);
                    return;
                }
            }
        }
    }
}
  1. Save the file
  2. Hit play, and enjoy!

Optional: You can push mouse integration further by adding additional bindings in your “Gameplay” and “UI” action maps, under actions like “Interact”, “FireAbilityX”, “Skip”, and “Cancel”

💬 Comments (4)

FTLJustin Jan 15, 2025 at 10:26 PM
Nice! This should help a ton of people who ask for this feature!
💯 3
Gyvr Jan 15, 2025 at 10:27 PM
For sure!!
jingyixing Jan 19, 2025 at 05:06 AM
Now I find a problem,In general games, you can change the key bindings in the game playing. How do you achieve this? If you don’t want to, you can give some suggestions.
Gyvr Jan 19, 2025 at 05:11 AM

There are several assets that can add that feature to your game, like this one: https://prf.hn/click/camref:1100l3L8uv/destination:https://assetstore.unity.com/packages/tools/gui/settings-game-options-unified-menu-240015

Otherwise it should be fairly easy to implement and make it work with any games, as Unity’s new input system (which is what Mythril2D uses) is very permissive

Want to continue the conversation?