Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by Bokaii · Aug 07, 2018 at 06:24 AM · uicursoreventsystem

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by LarsBlack · Aug 07, 2018 at 06:28 AM

Did you try using OnPointerEnter/OnPointerExit?

https://docs.unity3d.com/ScriptReference/UI.Selectable.html

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bokaii · Aug 07, 2018 at 06:32 AM 0
Share

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?

avatar image LarsBlack Bokaii · Aug 07, 2018 at 06:51 AM 0
Share

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.

avatar image Bokaii LarsBlack · Aug 07, 2018 at 06:55 AM 0
Share

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"); }

Show more comments
avatar image
2

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image LarsBlack · Aug 07, 2018 at 07:18 AM 0
Share

Glad to hear that it finally work.

avatar image
2

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;
     }
 
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1
Wiki

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. c 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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image W_I_L_L · Jun 21, 2019 at 10:11 PM 0
Share

Image

avatar image W_I_L_L · Jun 21, 2019 at 10:14 PM 0
Share

I couldn't get the image to upload here is an online image (it doesn't have what i did though)Image

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

153 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to detect UI Elements with Raycast 1 Answer

Is there a way to fire the inspector events from code? 0 Answers

Button.Select(); does not highlight? 12 Answers

Cursor Toggle in Menu (UI) 1 Answer

How to get the fingerID (Touch) that is pressing a specific UI button. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges