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
2
Question by Necronomicron · Jul 23, 2016 at 11:43 AM · uimousemouseover

How can I get UI element over which pointer is in Unity3D?

I can detect if mouse is hovering any UI element by EventSystem.current.IsPointerOverGameObject().

But how do I know which GameObjest it is exactly?

I have tried:

     if (EventSystem.current.IsPointerOverGameObject())
         foreach (GameObject go in new PointerEventData(EventSystem.current).hovered)
             print(go.name);

But in every moment new PointerEventData(EventSystem.current).hovered is empty whether I hover or not.

I can see required info using print(EventSystem.current);:

 <b>Selected:</b>


 <b>Pointer Input Module of type: </b>UnityEngine.EventSystems.StandaloneInputModule
 <B>Pointer:</b> -1
 <b>Position</b>: (746.0, 535.0)
 <b>delta</b>: (60.0, -44.0)
 <b>eligibleForClick</b>: False
 <b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject)
 <b>pointerPress</b>: 
 <b>lastPointerPress</b>: 
 <b>pointerDrag</b>: 
 <b>Use Drag Threshold</b>: True
 <b>Current Rayast:</b>
 Name: cursorInfoText (UnityEngine.GameObject)
 module: Name: Canvas (UnityEngine.GameObject)
 eventCamera: 
 sortOrderPriority: 0
 renderOrderPriority: 0
 module camera: null
 distance: 0
 index: 0
 depth: 1
 worldNormal: (0.0, 0.0, 0.0)
 worldPosition: (0.0, 0.0, 0.0)
 screenPosition: (746.0, 535.0)
 module.sortOrderPriority: 0
 module.renderOrderPriority: 0
 sortingLayer: 0
 sortingOrder: 0
 <b>Press Rayast:</b>


 <B>Pointer:</b> -2
 <b>Position</b>: (746.0, 535.0)
 <b>delta</b>: (60.0, -44.0)
 <b>eligibleForClick</b>: False
 <b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject)
 <b>pointerPress</b>: 
 <b>lastPointerPress</b>: 
 <b>pointerDrag</b>: 
 <b>Use Drag Threshold</b>: True
 <b>Current Rayast:</b>
 Name: cursorInfoText (UnityEngine.GameObject)
 module: Name: Canvas (UnityEngine.GameObject)
 eventCamera: 
 sortOrderPriority: 0
 renderOrderPriority: 0
 module camera: null
 distance: 0
 index: 0
 depth: 1
 worldNormal: (0.0, 0.0, 0.0)
 worldPosition: (0.0, 0.0, 0.0)
 screenPosition: (746.0, 535.0)
 module.sortOrderPriority: 0
 module.renderOrderPriority: 0
 sortingLayer: 0
 sortingOrder: 0
 <b>Press Rayast:</b>


 <B>Pointer:</b> -3
 <b>Position</b>: (746.0, 535.0)
 <b>delta</b>: (60.0, -44.0)
 <b>eligibleForClick</b>: False
 <b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject)
 <b>pointerPress</b>: 
 <b>lastPointerPress</b>: 
 <b>pointerDrag</b>: 
 <b>Use Drag Threshold</b>: True
 <b>Current Rayast:</b>
 Name: cursorInfoText (UnityEngine.GameObject)
 module: Name: Canvas (UnityEngine.GameObject)
 eventCamera: 
 sortOrderPriority: 0
 renderOrderPriority: 0
 module camera: null
 distance: 0
 index: 0
 depth: 1
 worldNormal: (0.0, 0.0, 0.0)
 worldPosition: (0.0, 0.0, 0.0)
 screenPosition: (746.0, 535.0)
 module.sortOrderPriority: 0
 module.renderOrderPriority: 0
 sortingLayer: 0
 sortingOrder: 0
 <b>Press Rayast:</b>

By the way why are there 3 pointers (-1, -2, -3) and what does this mean? I've read somewhere in docs, pointer -1 is left mouse button, but I click nothing, so this is weird.

I can see <b>pointerEnter</b>: cursorInfoText (UnityEngine.GameObject) and Name: cursorInfoText (UnityEngine.GameObject) which is what I need. But how do I extract this info? I have tried print(new PointerEventData(EventSystem.current).pointerEnter);, but it is Null doesn't matter I enter or hover. And I see no other appropriate methods or properties in docs, although I can see that info is stored. I've tried EventSystem.current.currentSelectedGameObject too and it always returns Null as well. What have I missed?

My goal is to detect if mouse is hovering UI element with some conditions (ignore small amount of UI elements (likely by tag) and don't ignore the rest (most of elements)), so I have to get GameObject itself. And if mouse doesn't hover UI element (except those few), script does unrelated to UI stuff. But do not ignore if it hovers one of those few and one of the rest at the same time.

Unity 5.3.4.

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

2 Replies

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

Answer by Roland1234 · Jul 28, 2016 at 07:04 AM

Simply newing up a PointerEventData object will not initialize it with the actual data you're looking for. It sounds to me like you're wanting to get the PointerEventData from your current input module. Problem is that the relevant input module members are not publicly exposed - but they do have protected access, so you can derive your own input module and expose them that way. For example, if you're using the default StandaloneInputModule:

 using UnityEngine.EventSystems;
 
 public class ExtendedStandaloneInputModule : StandaloneInputModule
 {
     public static PointerEventData GetPointerEventData(int pointerId = -1)
     {
         PointerEventData eventData;
         _instance.GetPointerData(pointerId, out eventData, true);
         return eventData;
     }
 
     private static ExtendedStandaloneInputModule _instance;
 
     protected override void Awake()
     {
         base.Awake();
         _instance = this;
     }
 }

Use something like the above to replace the input module in your EventSystem object (may have to set "Force Module Active" for it to work), and you can call the static ExtendedStandaloneInputModule.GetPointerEventData() method to get proper PointerEventData objects from it. In your case you will likely be wanting to look at the hovered member which is a List<GameObject> of objects in the hover stack.

The EventSystem keeps a dictionary of PointerEventData objects keyed by pointer ids, where the pointer ids refer to specific pointer events: -1, -2, and -3 are left, right, and middle mouse buttons; and I believe 1, 2, 3+ refer to first, second, third, touch input fingers, etc. but I'm not too familiar with touch controls. I'm assuming any mouse button pointer id will yield the hovered members you'll be interested in, if not then at least the default -1 pointer id should work.

Hope that helps!

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 Necronomicron · Jul 30, 2016 at 11:18 AM 0
Share

I can remember 0 is the first touch. :)

avatar image
-1

Answer by alex_ohadi · Apr 10, 2019 at 09:35 PM

Try this in your foreach loop

if(go.gameobject == this.gameobject) { // go.gameobject is not the UI element you are hovering over }

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

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

78 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

Related Questions

IPointerEnterHander only fires on click 0 Answers

GameObject rapidly changing position (flickering) 1 Answer

Put mouse to sleep 1 Answer

Get object at mouse position? (UI) 0 Answers

Re-center UI Image after zooming out 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