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
2
Question by Aridez · Oct 15, 2017 at 06:42 PM · uibuttonguistyleguiskinguilayout.button

GUILayout.button hover texture not changing immediately

I'm having a small issue with a button. I'm creating a custom editor window and I want to switch to another texture when hovering over the button, right now I'm using a GUISkin and changed there the "normal" and "hover" textures.

It's working, but there's a delay when hovering over the texture and when moving the mouse out. Instead of applying the change immediately, for example, if the mouse is moved quickly over the button it won't enter in the "hover" state and when leaving it will remain in the "hover" state for a fer moments.

Any ideas about why is this happening?

Thanks!

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

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

Answer by Bunny83 · Oct 15, 2017 at 06:53 PM

The Editor is only updated when something changes. AFAIK by default wantsMouseMove is set to false so you don't get mouse move events. (note that mouse drag is something different).

Also note that, as you can ready on the documentation page i've linked, the editor will not automatically repaint with each mouse move. You get a repaint after a few sec probably due to some internal tooltip event.

You generally should avoid repainting the window all the time. If this is a custom editor for your personal use you can force a repaint when you receive a MouseMove event

 if (Event.current.type == EventType.MouseMove)
     Repaint();

This should repaint your window as soon as the mouse moves. Though something like that should be avoided if you plan to release it as part of an asset to the public.

Comment
Add comment · Show 7 · 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 Aridez · Oct 15, 2017 at 07:31 PM 0
Share

Thanks for replying, that makes things much more clear. To follow up my question, would there be a "good" way to make a button change when hovering over it?

avatar image Bunny83 Aridez · Oct 15, 2017 at 10:31 PM 0
Share

Well, if it's just one button you can check manually if the mouse position is over the button and repaint when you enter / leave the area of the button.

If you have multiple buttons you can create a wrapper like this:

 private static int m_HoverControlID = 0;
 public static bool HoverButton(GUIContent aContent, GUIStyle aStyle, params GUILayoutOption[] aOptions)
 {
     var e = Event.current;
     bool res = GUILayout.Button(aContent, aStyle, aOptions);
     int id = GUIUtility.GetControlID(aContent, FocusType.Passive);
     if (e.type == EventType.$$anonymous$$ouse$$anonymous$$ove)
     {
         if (GUILayoutUtility.GetLastRect().Contains(e.mousePosition))
         {
             if (m_HoverControlID != id)
             {
                 m_HoverControlID = id;
                 if (EditorWindow.mouseOverWindow != null)
                     EditorWindow.mouseOverWindow.Repaint();
             }
         }
         else
         {
             if (m_HoverControlID == id)
             {
                 m_HoverControlID = 0;
                 if (EditorWindow.mouseOverWindow != null)
                     EditorWindow.mouseOverWindow.Repaint();
             }
         }
     }
     return res;
 }

"m_HoverControlID" will track the GUI element that is currently under the cursor. It helps to detect when you enter the area of a gui element or when you leave it. In both cases it will call Repaint on the editorwindow under the mouse cursor.

I haven't tested it but it should work. Note that the id i use is a seperate ID from the usual control ID. It's only used to track the hover state, nothing else.

avatar image Demigiant Bunny83 · Feb 09, 2020 at 11:40 AM 0
Share

Sorry for the necro, but I think there must be another way except I can't find it. I mean: using the default Unity button's GUIStyle will show the hover correctly regardless of wants$$anonymous$$ouse$$anonymous$$ove, it's when you change to a custom style that it doesn't work anymore. Am trying to find a reason for this.

Show more comments
avatar image
1

Answer by Tortuap · Nov 10, 2020 at 10:08 AM

I found that legacy styles have a different behavior when used, including the auto repaint-trigger mentioned in this thread, and that this based on their names. If you use a style copied from a legacy style, for ex. myStyle = new GUIStyle ( "Button" ), without renaming it, it will still somehow (unless you set the normal.background texture) use the legacy "Button" textures & auto repaint-trigger. This is nuts.

Comment
Add comment · 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
0

Answer by wesleywh · Sep 20, 2020 at 09:39 PM

I know this is super old but it took me a long time to get something that works. Also this is the first and really only question about this. Just call

 EditorUtility.SetDirty(target);

At the end of

 OnInspectorGUI()

To force a repaint. Works fine for me.

Comment
Add comment · 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

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

133 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

Related Questions

Unable to find style in skin repaint 1 Answer

Whats wrong with my GUI.Toggle? 2 Answers

GUILayout.Button image 0 Answers

How to attach GUI Skin on button? 0 Answers

Destory button on click and other problem 0 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