- Home /
Clearing mouse hover effect when Cursor is hidden
Hi,
I've made my game to hide the cursor in menu when the user starts using keyboard or gamepad/controller. I also disable the mouse input for the UI. If the user moves the mouse, the cursor appears and mouse control is enabled.
There's only one thing that's bugging me. If a cursor/pointer is hovering over a button and then if I start using keyboard or gamepad for input, the pointer hides just fine, but the hovered button still has a hovered/selected appearance. So it just sits there looking selected even though I'm selecting other buttons with keyboard/gamepad.
It just looks silly, as if two buttons are selected.
Does anyone know how can I clear the effect of mouse hover once I hide and disable the cursor?
I didn't even manage to get the gameobject that is hovered.
(PS Similarly, if a button is selected by keyboard/gamepad, and i start using the mouse, then again I have the same silly appearance as if two buttons are selected (one remains selected and the other is hovered). I've solved this problem easily by calling EventSystem.current.SetSelectedGameObject(null) when mouse control is activated. Then again, if I start using keyboard/controller, I manually set SelectedGameObject to some default button for that menu panel.)
Answer by Marceta · Jan 21, 2016 at 11:22 AM
using UnityEngine;
using UnityEngine.EventSystems;
using System.Collections;
using System.Collections.Generic;
public class FirstMacScript : MonoBehaviour
{
void Update ()
{
PointerEventData pointer = new PointerEventData(EventSystem.current);
pointer.position = Input.mousePosition;
List<RaycastResult> raycastResults = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointer, raycastResults);
if(raycastResults.Count > 0)
{
Debug.Log (raycastResults [raycastResults.Count-1].gameObject);
}
}
}
Thanks!
So, using $$anonymous$$arceta's answer, I was able to unhover the button once keyboard/controller is used, like this:
void $$anonymous$$eyboardOrControllerUsed(){
if (Cursor.visible) {
Cursor.visible = false;
allow$$anonymous$$ouseInput = false;
ClearPointer$$anonymous$$ess();
}
}
public void ClearPointer$$anonymous$$ess() {
PointerEventData pointer = new PointerEventData(EventSystem.current);
pointer.position = Input.mousePosition;
List<RaycastResult> raycastResults = new List<RaycastResult>();
EventSystem.current.RaycastAll(pointer, raycastResults);
if (raycastResults.Count > 0) {
foreach (RaycastResult raycastResult in raycastResults) {
Debug.Log(raycastResult.gameObject.name);
GameObject hoveredObj = raycastResult.gameObject;
if (hoveredObj.GetComponent<Button>()) {
hoveredObj.GetComponent<Button>().OnPointerExit(pointer);
} else {//sometimes in my setup the child image is interactable
hoveredObj = hoveredObj.transform.parent.gameObject;
if (hoveredObj.GetComponent<Button>()) {
hoveredObj.GetComponent<Button>().OnPointerExit(pointer);
}
}
}
}
}
Hey Thanks, it's a year later, but I had the exact same problem, and this solution worked.
Answer by Charlivi · Mar 02 at 03:56 PM
Old thread, but I ended up here for the same question in 2022.
The answer from wlad_s is still working (thanks again), but I found another solution that works for me and wanted to share it in case it can help someone else.
I'm using Rewired, but pretty sure it can be done without it too.
// Put this code in a Controller changed callback
if (controller.type != ControllerType.Mouse)
{
Cursor.lockState = CursorLockMode.Locked;
Cursor.visible = false;
}
else if (controller.type == ControllerType.Mouse)
{
Cursor.lockState = CursorLockMode.None;
Cursor.visible = true;
}
Your answer
Follow this Question
Related Questions
Multiple Players on one Computer/Console 5 Answers
Standalone borderless window doesnt get keyboard input (Mac) 0 Answers
Issues with mouse input after resizing Standalone 1 Answer
New Input System: Event on input type changed (between mouse, keyboard and controller) 1 Answer
Input System Can't Catch Event on Update 0 Answers