Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 /
  • Help Room /
This question was closed Jan 09, 2016 at 09:14 PM by Le-Pampelmuse for the following reason:

The question is answered, right answer was accepted.

avatar image
25
Question by Anymeese · Sep 02, 2014 at 05:43 AM · uiguibuttonmouse4.6

(4.6 UI) How to detect mouse over on button?

How can you call a function from a script when a button is moused over?

I tried OnMouseEnter/Exit, even WITH a collider attached to the button. I also dont see any way to make it work like on CLICK does in the inspector, although that would be very convenient!

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

3 Replies

  • Sort: 
avatar image
105
Best Answer

Answer by HarshadK · Sep 02, 2014 at 06:40 AM

Method:

  1. Add an Event Trigger component to your button game object.

  2. Click on Add New button and select PointerEnter.

  3. Now click on '+' button to add a new item to the list of event of type PointerEnter(BaseEventData).

  4. Select the object containing your script.

  5. Now select the function to be called from the list of functions.

Comment
Add comment · Show 11 · 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 ispeelgood · Oct 26, 2014 at 04:08 AM 0
Share

thank you! this is the best solution.

avatar image perchik · Nov 12, 2014 at 03:46 AM 0
Share

This. Thank you greatly!

avatar image 3ddave · Feb 01, 2015 at 08:20 PM 0
Share

This seems to be the cleanest way to do so, but if you have stacked objects in your GUI then this will only trigger on the top-most object. In the case of stacked objects you can use the RectTransform to deter$$anonymous$$e the world coordinates of the bounding box and take action as appropriate from there.

avatar image TheLagbringer · Jul 10, 2015 at 07:37 PM 0
Share

This is god solution, thanks a lot. I was getting worried there was no way around making your own fishy routine in Update(), or something similar...

avatar image Jeff_B · Jul 25, 2015 at 01:45 PM 0
Share

Another thank you. Big help.

Show more comments
avatar image
3

Answer by RKSandswept · Oct 12, 2014 at 03:23 AM

Here is my solution. I fire a ray throught the UI and see what it hits. If it is a button etc. then I am in "Entering Text" mode which also covers hovering over a buttone and such. I also check the EventSystem.current.currentSelectedGameObject in case they are entering text in a field and have moved the mouse out of the field.

I also have a canvas called BackgroundEventCatcher which catches button click that get through the UI. I also have a class for showing notifications on the bottom edge of the screen.

So here it is, (Unity3d 4.6 rev 20) ....

[CODE]

     PointerEventData pe = new PointerEventData(EventSystem.current);
     pe.position =  Input.mousePosition;
 
     List<RaycastResult> hits = new List<RaycastResult>();
     EventSystem.current.RaycastAll( pe, hits );

     bool hit = false;
     GameObject hgo = null;
     string gos = "gos: ";
     foreach(RaycastResult h in hits)
     {
         GameObject g = h.go;
         gos += g + "  ";
         hit = ( g.name != "BackgroundEventCatcher" &&
                 (g.GetComponent<Button>() ||
                  g.GetComponent<Canvas>() ||
                  g.GetComponent<InputField>())
                );
         if(hit)
         {
             break;
         }
     }

     if( hit ||
         EventSystem.current.currentSelectedGameObject != null &&
         EventSystem.current.currentSelectedGameObject.name != "BackgroundEventCatcher" &&
        (EventSystem.current.currentSelectedGameObject.GetComponent<Button>() != null ||
         EventSystem.current.currentSelectedGameObject.GetComponent<Canvas>() != null ||
         EventSystem.current.currentSelectedGameObject.GetComponent<InputField>() != null) )
     {
         keyMode = eKeyInputMode.EnteringText;
     }
     else
     {
         keyMode = eKeyInputMode.KeysAreCommands;
     }

     Notifications.ShowNotification("hit : " + hit + " of " + hits.Count + " :  " + keyMode + " :  gos " + gos);

[/CODE]

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 darkoleptiko · Jan 19, 2015 at 03:09 AM 0
Share

Nice! I was just looking for a way to fire custom RayCasts to the ui

avatar image Clevereen · Nov 26, 2020 at 07:16 PM 0
Share

Just wanted to say thank you! I used it to prevent the player to move a gameObject when he was click on a UI!

avatar image
2

Answer by srazu · Dec 31, 2014 at 07:10 PM

 var EventSystem : UnityEngine.EventSystems.EventSystem;
 
 function Start () {
     EventSystem = GameObject.Find("EventSystem").GetComponent(UnityEngine.EventSystems.EventSystem);
 }
 
 function MyClickFunction () {
     if (EventSystem.IsPointerOverGameObject())
         return;
     else
         DoAnythingInTheWorld();
 }
 
 
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 ketura · Sep 01, 2015 at 11:03 PM 1
Share

Also note that you can simply call

 if(EventSystem.current.IsPointerOverGameObject())

if it's just going to be in event-based code. Though you should obviously cache it like you're doing if you're going to have it Update() somewhere.

Follow this Question

Answers Answers and Comments

30 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

Related Questions

Button mouseover only works when other canvas is active 0 Answers

Best way to handle multiple buttons? (C#) 3 Answers

How To input text from a button? 1 Answer

Using an UI Image as a Mouse Alternative 0 Answers

UI Text Editing Problem, What's Wrong? Need Anybody to Assist 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