- Home /
Check if mouse is over UI
How do I check if the cursor is hovering over a ui image?
I've tried EventSystem.IsPointerOverGameObject, but I couldn't get it to work. Is there another way? or can someone guide me through how to use the EventSystem.IsPointerOverGameObject?
Thank you.
Answer by LarsBlack · Aug 07, 2018 at 06:28 AM
Did you try using OnPointerEnter/OnPointerExit?
I can't get it to work, but maybe I'm doing it wrong. How would I use those functions to check if my mouse is hovering over a ui image for example?
Try doing the following:
-Add to your Ui element the component "Event trigger", add the event type OnPointerEnter/OnPointerExit depending on what you want to do.
-Add a script with a function that does for example:
Debug.log("enter UI element");
I tried this with a simple game that I'm making, and when the cursor is hovering over the UI elements (in my case are piano keys), the debug message is promted.
I just tried it. I didn't work. Does the function have to be something specific? or can I just put:
public void Test() { print("Test"); }
Answer by Bokaii · Aug 07, 2018 at 07:02 AM
I figured out why I couldn't use the OnPointerEnter/Exit.
In my FirstPersonController I had Cursor.lockState = CursorLockMode.Locked, and that's why it couldn't register when the cursor was over the ui image. I fixed it now.
Answer by Sjonsson · Mar 24, 2019 at 11:18 PM
I did a script that you can add to every Canvas. Then you can reach the static bool MouseInputUIBlocker.BlockedByUI from anywhere to see if the mouse is over UI or not.
This is the same as LarsBlack's setup but it's automatic.
I know it's dirty with the static bool I only did this as a quick fix but it might help someone:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
[RequireComponent(typeof(EventTrigger))]
public class MouseInputUIBlocker : MonoBehaviour
{
public static bool BlockedByUI;
private EventTrigger eventTrigger;
private void Start()
{
eventTrigger = GetComponent<EventTrigger>();
if(eventTrigger != null)
{
EventTrigger.Entry enterUIEntry = new EventTrigger.Entry();
// Pointer Enter
enterUIEntry.eventID = EventTriggerType.PointerEnter;
enterUIEntry.callback.AddListener((eventData) => { EnterUI(); });
eventTrigger.triggers.Add(enterUIEntry);
//Pointer Exit
EventTrigger.Entry exitUIEntry = new EventTrigger.Entry();
exitUIEntry.eventID = EventTriggerType.PointerExit;
exitUIEntry.callback.AddListener((eventData) => { ExitUI(); });
eventTrigger.triggers.Add(exitUIEntry);
}
}
public void EnterUI()
{
BlockedByUI = true;
}
public void ExitUI()
{
BlockedByUI = false;
}
}
Answer by W_I_L_L · Jun 21, 2019 at 10:09 PM
@MrMelonPie You can try using the Event Trigger Component. https://docs.unity3d.com/Manual/script-EventTrigger.html The Event trigger allows you to call functions from scripts on the object. In this image I have the Event Trigger call a void that changes a Boolean (the Boolean is called mouseover it tells the script when the mouse is over the game object) when the mouse is over it.
I couldn't get the image to upload here is an online image (it doesn't have what i did though)