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
8
Question by melwei · Jun 07, 2016 at 05:05 PM · c#uibuttonmouseover

OnMouseOver UI Button c#

How to detect in script, if the mouse is over a script-created button? If any component is needed, how can you create the object and set the component's settings in the right way?

Comment
Add comment · Show 1
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 dude4004 · Jun 15, 2017 at 01:20 PM 0
Share

Work like a charm

2 Replies

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

Answer by Flaring-Afro · Jun 07, 2016 at 05:26 PM

You don't use OnMouseOver for UI elements, use IPointerEnterHandler Then attach script to game object that you want to implement it on.

example:

 using UnityEngine.EventSystems;
 
 public class MyClass: MonoBehaviour, IPointerEnterHandler{
     
     public void OnPointerEnter(PointerEventData eventData)
     {
         //do stuff
     }

Comment
Add comment · Show 3 · 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 JasonSpine · Mar 26, 2018 at 01:41 PM 6
Share

Wrong! OnPointerEnter executes only once, while On$$anonymous$$ouseOver executes every frame.

avatar image AP124526435 · Sep 10, 2019 at 09:54 AM 0
Share

Do you know why Unity choose to duplicate functionality like On$$anonymous$$ouseEnter? And also why they didn't implement an equivalent IPointerXyzHandler to On$$anonymous$$ouseOver?

avatar image AP124526435 · Sep 10, 2019 at 10:00 AM 0
Share

@JasonSpine I've implemented the analogous function: http://answers.unity.com/answers/1664400/view.html It's a shame they made this design choice. Would be interesting to know why.

avatar image
16

Answer by AP124526435 · Sep 10, 2019 at 09:59 AM

I'm building on Flaring-Afro's answer. IPointerEnterHandler works but is not the same as OnMouseOver. To get this behaviour you (unfortunately) also need IPointerExitHandler, a private bool and the Update function such as:

 public class UIElement : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler
 {
     private bool mouse_over = false;

     void Update()
     {
         if (mouse_over)
         {
             Debug.Log("Mouse Over");
         }
     }
 
     public void OnPointerEnter(PointerEventData eventData)
     {
         mouse_over = true;
         Debug.Log("Mouse enter");
     }
 
     public void OnPointerExit(PointerEventData eventData)
     {
         mouse_over = false;
         Debug.Log("Mouse exit");
     }
 }


Comment
Add comment · Show 3 · 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 mateov951 · Nov 22, 2019 at 05:26 AM 0
Share

Thanks! Worked like a charm!

avatar image Bux777 · Jun 03, 2020 at 10:48 AM 0
Share

You da man!

avatar image mrCrush · Jan 09 at 10:31 AM 0
Share

Thank you! That's very helpful!

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

171 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 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

UI works in editor, but not on mobile device 1 Answer

I made an resume button, but I don't know how to code it to close my pause menu, any tips? 2 Answers

I need to move a sprite when an UI button is clicked 1 Answer

Change scene and play it by clicking UI Button? Scene does just load but not start playmode? 1 Answer

Unity - destroy clone child object (button) 1 Answer


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