• 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 DryTear · Sep 11, 2013 at 10:23 PM · gui.buttononhover

How do I know if GUI is selected?

Is there a way to find out if the GUI.Button texture has changed to hover? Or anyway to find out if the mouse is over the button.

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Benproductions1 · Sep 11, 2013 at 11:14 PM

Hello,

Unity's GUI is very crappy and should only get used for prototyping (at least until 4.2 is released). On that note, there is no built in way of finding out the buttons state like there is (and should be) in any modern GUI modules.

There is however, a workaround:
Instead of finding out the buttons state, you calculate for yourself if the mouse is over the button:

 //finding wether mouse is in rectangle
 function InRect(rectangle:Rect) {
     return Input.mousePosition.x > rectangle.x &&
         Input.mousePosition.x < rectangle.x + rectangle.width &&
         Input.mousePosition.y > rectangle.y &&
         Input.mousePosition.y < rectangle.y = rectangle.height;
 }
 
 //using GUI
 var rect:Rect = Rect(0, 0, 100, 200);
 function OnGUI() {
     GUI.Button(rect, /*content*/);
     if (InRect(rect)) {
         //we are over the button
     }
 }
 
 //using GUILayout
 function OnGUI() {
     GUILayout.Button(/*content*/)
     if (InRect(GUILayoutUtility.GetLastRect())) {
         //we are over the button
     }
 }

Note that this code is untested, so there might be some bugs :)

Hope this helps,
Benproductions1

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 DryTear · Sep 12, 2013 at 10:29 PM 0
Share

Then how does the engine change the texture? there has to be a way to find out, but using mouse over rect is a good idea, thanks. I was also wanting to use it for resizing, but I see that i would then have to create more functions for each button, that sucks.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

16 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

Related Questions

Screen moves on clicking the button 1 Answer

How to add multiple lines of text in GUI.Button with different font styles? 0 Answers

Setting a GUI.Buttons transparency 1 Answer

How to change GUI.Button Texture? 2 Answers

Button clickzone is only small area of the button 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges