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 Grady · Jul 05, 2011 at 12:58 PM · guimouseboxhoverelement

How to check if player is hovering mouse over a GUI Box

Hey guys,

I was wondering, is it possible to simply check if the user is hovering their mouse over a gui box?

Thanks

-Grady

Comment
Add comment · Show 4
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 DavidDebnar · Jul 05, 2011 at 01:29 PM 0
Share

Is that GUI box created with code? Or just a GUI Texture?

avatar image Grady · Jul 05, 2011 at 01:32 PM 0
Share

yeah it's created with code...

avatar image SasugaShogun · Aug 26, 2013 at 06:12 PM 0
Share

Why are we reinventing the wheel? Unity already knows whether or not the mouse is inside the button, it changes colors, it 'activates' when 'pressed'/'clicked'... Why can't we access that information?

avatar image sdgd · Aug 26, 2013 at 06:18 PM 0
Share

oh GR8 we can now convert answers to comments with 1$$anonymous$$ karma, ... did mistake by getting @SasugaShogun published, thought it was a question

thanks UNITY.

AND @SasugaShogun do not post an answer as a comment

3 Replies

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

Answer by DavidDebnar · Jul 05, 2011 at 01:36 PM

If you want to use a GUI Box or any other OnGUI element whose position and size is defined by a Rect, you need to realise that GUI elements are in GUI coordinates (0,0 = top-left), where Input.mousePosition is in Screen, or pixel coordinates (0,0 = bottom-left). This means you can do one of two things.

The first solution is the easier of the two - Events. Events and OnGUI are very tightly connected, so they share the same coordinate system.

 var mousePosFromEvent : Vector2 = Event.current.mousePosition;

The second solution, if you for some reason don't want to use events, is to flip the y axis of Input.mousePosition.

 var mousePosInGUICoords : Vector2 = new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y);


You can read about Input.mousePosition here. Or read more about Events here and here.

(thanks for robertbu for pointing out that pixel and gui coords don't match.)

function OnGUI () {

 var rect1 : Rect = Rect(240,240,320,150);
 var rect2 : Rect = Rect(0,0,300,500);
 
 GUI.Box(rect1, "Rect1");
 GUI.Box(rect2, "Rect2");
 
 if(rect1.Contains(Event.current.mousePosition))
     Debug.Log("rect1");
 else if(rect2.Contains(Event.current.mousePosition))
     Debug.Log("rect2");
 else
     Debug.Log("no rect");

}

---

If you don't mind using GUITextures, you can use the MonoBehaviour.OnMouseEnter() function.

function OnMouseEnter () {
   print("There is a mouse on me!");
}
  • David

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 Grady · Jul 05, 2011 at 01:58 PM 0
Share

thanks!!!!!!

-Grady

avatar image Tsurugi21 · Jan 13, 2015 at 01:21 AM 0
Share

You dude just made my day ^^

avatar image
2

Answer by chargedneuron · Jun 04, 2013 at 03:55 AM

I had to solve an issue like this today. I thought I would post my answer here so help others out...

On MouseEnter start a coroutine that waits whatever you want for a hover delay. Once the coroutine expires process your OnHover task; starting with a check to see that the mouse is within the rect before continuing. This will make sure we are still hovering as the timer expires. Upon MouseExit stop the coroutine.

  float hoverDelay = 5;
  Rect rectIcon; // The Rect for the GUI.Texture we are hovering over.
     
 void OnMouseOver ()
 {
     StartCoroutine( "Wait", hoverDelay );
 }
         
 void OnMouseExit ()
 {
     StopCoroutine("Wait");
 }
         
 private IEnumerator Wait (float seconds)
 {
     yield return new WaitForSeconds(seconds);
         if (rectIcon.Contains (Input.mousePosition)) {
             //Do OnHover Stuff here. Or set a bool to trigger an event elsewhere.
        }
 }
         
 
Comment
Add comment · Show 6 · 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 robertbu · Jun 04, 2013 at 03:58 AM 0
Share

This (and the other solutions on this page) will not work correctly assu$$anonymous$$g you are using GUI to display the box. 'rectIcon' will be in GUI coordinates. Input.mousePosition will be in screen coordinates. The "right" way to solve this position is to use GUI events and look at Event.mousePosition (which will be in GUI coordinates).

avatar image chargedneuron · Jun 04, 2013 at 05:43 AM 0
Share

Respectfully - Perhaps you would be kind enough to provide a code example of the "right" way. Thank you.

avatar image robertbu · Jun 04, 2013 at 06:47 AM 0
Share
 #pragma strict
  
 private var rect : Rect = Rect(200, 200, 150, 50);
  
 function OnGUI() {
     var e = Event.current;
     if (rect.Contains(e.mousePosition))
         Debug.Log("I'm in the box");
 
     GUI.Box(rect, "Box");
 }

Note OnGUI() is often called many times a frame. I don't know what you need this functionality for, but you may need to put additional conditions on the check if you only want it to fire once per frame.

avatar image robertbu · Jun 04, 2013 at 06:54 AM 0
Share

Oh, and if you really need to use Input.mousePosition, the conversion between GUI coordinates:

 var v3GUI = Camera.main.mousePosition;
 v3GUI.y = Screen.height - v3GUI.y;

v3GUI can then be used in a Rect.Contains() call for a rect used for GUI output. But handling the mouse position using Event.current is a better way to handle the situation if you can.

avatar image chargedneuron · Jun 04, 2013 at 06:55 AM 0
Share

I understand that part. $$anonymous$$y intention is to have an inventory item (as a GameObject.GUITexture) with a Rect. As the mouse enters the Rect I show the name of the Inventory Item with a GUI.Label at the mouse point. After 3 Seconds (via my coroutine) I replace the GUILabel with a detailed description of what the object is. If the mouse exits the Rect before, during, or after the Coroutine has fired I stop the coroutine to restart the 3 second delay.

I use the coroutine function to set a Bool that tells the OnGUI() function which GUI.Label to show.

As I understand your example you are suggesting I use the result of the Event.Current rather than the Rect.Contains functions?

Show more comments
avatar image
1

Answer by DanjelRicci · Jul 05, 2011 at 01:32 PM

There is what you need here in the Unity guide: Rect.Contains. Example:

//Check if the mouse is inside a Rect  
var mouseInside = false;
function Update () {
    var rect = Rect (0, 0, 150, 150);
    if (rect.Contains(Input.mousePosition))
        mouseInside = true;
    else
        mouseInside = false; 
}

Just use a Rect wich represents your GUI box and you are done.

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 tteneder · May 17, 2013 at 01:39 PM 0
Share

great answer, but you really can spare the if: var mouseInside = false; var rect = Rect (0, 0, 150, 150); function Update () { mouseInside = rect.Contains(Input.mousePosition)); }

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

9 People are following this question.

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

Related Questions

GUI.Button on MouseHover 1 Answer

How to make a Hover event on GUI.Button 6 Answers

Detecting Mouse Over Using Tooltip 2 Answers

detect mouseover with grid buttons? 1 Answer

How To Get Current Mouse Position and have a GUI Box on right clicked 2 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