Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by thenachotech1113 · Dec 16, 2014 at 10:24 PM · buttonmousecheckover

how to check if cursor is over as button?

Hello everyone, I want to have a button to purchase an item, but to save space i would like to not show all the stats of that item. Thought the aproach many games have taken to have it so when you place your cursor over a certain button it gives you a box with stats or a description of sort. Thing is the only way I can think of is to check if the cursor is in a certain x, y coordinate in the screen it will create a box. This though, seems awefully complicated, so i was wondering if there´s a better way to do this? I apreciate all your time and thanks in advanced.

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
1
Best Answer

Answer by Cherno · Dec 16, 2014 at 11:55 PM

First of all, to check if a cursor in over a button, you can just use Rect.Contains:

      if(buttonRect.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y), true)) {
           //
      }


Now for the second part of your question. You could create a custom Label, window, or whatever with the needed inormation in it at your mouse position, or any other position on screen, whenever the buttonRect.Contains check is true.

You can also use Unity's built-in ToolTip function which automatically shows a box whenever your cursor is above any GUI element that has GUIContent which in turn has a tooltip string specified:

 if(GUI.Button(buttonRect, new GUIContent("button string", buttonTexture, "ToolTip string"))) {
 
 }

You make the Tooltip show up by calling this code inside your OnGUI() function:

 if (!String.IsNullOrEmpty(GUI.tooltip)) {
      GUIContent tooltipContent = new GUIContent(GUI.tooltip);
      GUIStyle style = new GUIStyle(GUI.skin.textArea);
      style.wordWrap = true;
      float toolTipHeight = style.CalcHeight(tooltipContent, 200f);
      float toolTipHeight2 = GUI.skin.label.CalcHeight(tooltipContent, 200f);
      Vector2 tooltipPos;
      if(Input.mousePosition.x > Screen.width - 200) {
           tooltipPos.x = Input.mousePosition.x - 200 + 16;
      }
      else {
           tooltipPos.x = Input.mousePosition.x + 16;
      }
             
      if(Input.mousePosition.y < toolTipHeight) {
           tooltipPos.y = Screen.height - Input.mousePosition.y - toolTipHeight + 16;
      }
      else {
           tooltipPos.y = Screen.height - Input.mousePosition.y + 16;
      }
      //the actual tooltip
      GUI.Label(new Rect(tooltipPos.x, tooltipPos.y, 200, toolTipHeight), GUI.tooltip, "Tooltip");
 }

Note that only the last line of code actually deals with showing the tooltip; the rest above it is additional but very useful code that a) makes sure that the tooltip box is always completely on screen and b) resizes it's height dynamically depending on the string size (without this, the text would just be cut at some point).

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 thenachotech1113 · Dec 17, 2014 at 02:31 PM 0
Share

thanks a lot.

avatar image
1

Answer by Kiwasi · Dec 17, 2014 at 12:11 AM

Are you using UI or legacy?

With the UI tools you simply add an event trigger to your button, add OnPointerEnter and OnPointerExit. Add a listener to each to display and hide your Tool Tip.

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 thenachotech1113 · Dec 17, 2014 at 02:30 PM 0
Share

oh, interesting. thanks going to try it out.

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

29 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

Related Questions

when mouse pass over a button,the console print some content 1 Answer

Change GUI on mouse over 1 Answer

Button being pressed but an other button gets the effect. 0 Answers

Locking levels on a level map? 1 Answer

GUI Button OnMouseUp? 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