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
2
Question by beakr · May 07, 2013 at 03:04 PM · colorguistylehoverguibutton

GUI Button text color not changing on hover

I have a little main menu with a GUIButton. I've made a GUIStyle for the button so that the button text should turn cyan on hover. There are no errors, but hovering over the button does not make it cyan. Here is my code (it's a C# script):

 GUIStyle MenuItemStyle = new GUIStyle();
 MenuItemStyle.normal.textColor = Color.white;
 MenuItemStyle.hover.textColor  = Color.cyan;
     
 int y = 200;
 GUI.Button(new Rect(Screen.width / 2 - 50, y, 150, 30), "CAMPAIGN", MenuItemStyle);
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
2
Best Answer

Answer by Ben Blaut · May 07, 2013 at 04:03 PM

Unless you want to completely define your style (backgrounds, colors for everything else, etc), you'll want to derive your style from a style that is already defined. You'll also want to make your GUIStyle variable public so you can assign its members. Something like this:

 public GUIStyle MenuItemStyle;

 void OnGUI()
 {
     MenuItemStyle = new GUIStyle(GUI.skin.button);
     MenuItemStyle.normal.textColor = Color.white;
     MenuItemStyle.hover.textColor  = Color.cyan;
     
     int y = 200;
     GUI.Button(new Rect(Screen.width / 2 - 50, y, 150, 30), "CAMPAIGN", MenuItemStyle);
 }
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
2

Answer by Max_Bol · Oct 27, 2014 at 11:10 PM

There is currently a small issue with the GUI.Button behavior (hover, active, focused, etc.) whenever you just uses text string : the behavior won't work unless a image is placed as background.

While this is an issue, it's not a bug. It's a limitation due to fonts not being seen as picture. (Fonts don't have the a predetermined Height and Width so Unity isn't able to "link" its height and width to the registered Rect.)

The most simple way of fixing this is to add, in the GUIStyle, a single image made of 4x4 pixels and saved with a pure black alpha channel. By setting its type to GUI, it will actually be an empty invisible texture with almost no impact on performances. By putting it as Background Image in the behavior dropdown menu (in the GUIStyle Inspector menu), Unity will then be able to give the Rect's sizes adjustement to the area and it will work as intended.

Just make sure that if you do something like adjustable font size based on the player's screen sizes that the Rect also adjust themselves, otherwise you'll end up with areas that doesn't fit the visual.

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

14 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

Related Questions

How to change text color on hover in new GUI? 10 Answers

Transferring Color from script on another GameObject to a GUI.Box 1 Answer

NullReferenceException when using GUIStyle to change text color 1 Answer

SelectionGrid layout and style issues 1 Answer

Can't create orange label 3 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