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
5
Question by mlepage · Apr 04, 2015 at 10:49 PM · guibuttontextcolorhover

How to change text color on hover in new GUI?

Using Unity 5 GUI, suppose I want text only menu buttons.

I make buttons with text children and disable the button's image script, so the button background does not draw, I see text only as desired.

However, the colors (normal, highlighted, pressed, disabled) do not apply to the text color. They seem to apply only to the image, which I disabled.

How can I have the text color change when the button UI is manipulated?

Comment
Add comment · Show 1
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 mlepage · Apr 04, 2015 at 10:50 PM 0
Share

Also a bit of a meta comment: there needs to be a sticky FAQ thingy explaining which tags to use for old GUI stuff vs. new GUI stuff. Too convoluted, too hard to search.

10 Replies

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

Answer by lordlycastle · Apr 04, 2015 at 11:34 PM

You can do by scripting, but that’s just painful. You should rather watch this video. It tells you how to create transitions when hovering over, or clicking etc an UI element.

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 mlepage · Apr 05, 2015 at 02:25 PM 0
Share

O$$anonymous$$ that video seems to be the best way to do it, thanks.

avatar image
18

Answer by jcv8000 · Apr 04, 2015 at 11:27 PM

You could use the button to receive the mouse hover. Add this script to each of the buttons:

 using UnityEngine;  
 using System.Collections;  
 using UnityEngine.EventSystems;  
 using UnityEngine.UI;
 
 public class MenuButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler {
 
     public Text theText;
 
     public void OnPointerEnter(PointerEventData eventData)
     {
         theText.color = Color.red; //Or however you do your color
     }
 
     public void OnPointerExit(PointerEventData eventData)
     {
         theText.color = Color.white; //Or however you do your color
     }
 }

(Sorry about the code not formatting correctly in the post)

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 mlepage · Apr 05, 2015 at 02:25 PM 0
Share

Thanks I can see how this would work.

avatar image simunovic100 · Jan 29, 2017 at 08:37 AM 0
Share

set the value with this code: this.GetComponentInChildren().color = Color.red; so you don't have to have the variable "theText" at all and set it in each button where you wish this to happen...

avatar image domussolisortum simunovic100 · Jan 30, 2017 at 03:32 AM 0
Share

simunovic100, see my fully plug-and-play answer on the second page...

avatar image simunovic100 domussolisortum · Jan 30, 2017 at 10:01 AM 0
Share

sorry, didn't see page 2...new here...you are right, your script looks good, we don't need "this" keyword as I wrote...thanx

Show more comments
avatar image $$anonymous$$ · Feb 12, 2018 at 12:13 AM 0
Share

Thanks for this code, it worked very well!

avatar image danhowe0 · Feb 17, 2020 at 02:53 AM 0
Share

I still get the message "Script needs to derive from monobehaviour" after using this code anybody know how to fix this?

avatar image
8

Answer by domussolisortum · Jan 06, 2017 at 04:59 PM

For posterity's sake, here's a C# script to change the color of Text on a GUI Button, inspired by jcv8000's script. The main difference is that this script is fully automatic and covers more cases than the others posted so far.

Features:

This script takes the text's original color into account and multiplies it by the color values that are already specified in the Button script and by the Button's color multiplier. The change in the Text's color will always match the change in the Button's color.

It has cases to handle the Button being disabled, and it even works if you've disabled the Button from an external script.

All you need to do is add the script to the Button object. No fuss.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 [RequireComponent (typeof(Button))]
 public class MenuButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
 {
  
     Text txt;
     Color baseColor;
     Button btn;
     bool interactableDelay;
 
     void Start ()
     {
         txt = GetComponentInChildren<Text>();
         baseColor = txt.color;
         btn = gameObject.GetComponent<Button> ();
         interactableDelay = btn.interactable;
     }
 
     void Update ()
     {
         if (btn.interactable != interactableDelay) {
             if (btn.interactable) {
                 txt.color = baseColor * btn.colors.normalColor * btn.colors.colorMultiplier;
             } else {
                 txt.color = baseColor * btn.colors.disabledColor * btn.colors.colorMultiplier;
             }
         }
         interactableDelay = btn.interactable;
     }
 
     public void OnPointerEnter (PointerEventData eventData)
     {
         if (btn.interactable) {
             txt.color = baseColor * btn.colors.highlightedColor * btn.colors.colorMultiplier;
         } else {
             txt.color = baseColor * btn.colors.disabledColor * btn.colors.colorMultiplier;
         }
     }
 
     public void OnPointerDown (PointerEventData eventData)
     {
         if (btn.interactable) {
             txt.color = baseColor * btn.colors.pressedColor * btn.colors.colorMultiplier;
         } else {
             txt.color = baseColor * btn.colors.disabledColor * btn.colors.colorMultiplier;
         }
     }
 
     public void OnPointerUp (PointerEventData eventData)
     {
         if (btn.interactable) {
             txt.color = baseColor * btn.colors.highlightedColor * btn.colors.colorMultiplier;
         } else {
             txt.color = baseColor * btn.colors.disabledColor * btn.colors.colorMultiplier;
         }
     }
 
     public void OnPointerExit (PointerEventData eventData)
     {
         if (btn.interactable) {
             txt.color = baseColor * btn.colors.normalColor * btn.colors.colorMultiplier;
         } else {
             txt.color = baseColor * btn.colors.disabledColor * btn.colors.colorMultiplier;
         }
     }
 
 }
Comment
Add comment · Show 4 · 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 ekergraphics · Mar 20, 2017 at 07:48 PM 0
Share

This is a neat script, and should definitely be default behavior in Unity.

Two comments, though: It doesn't take Fade Duration into account and it doesn't take the Pressed Color into account.

avatar image domussolisortum ekergraphics · Mar 21, 2017 at 04:55 AM 0
Share

Thanks for your reply. I have intended to incorporate Fade Duration but have yet to decide on the optimal method of transitioning between two colors. As for Pressed Color I'm not certain that's different from OnPointerDown. What I would also like to do is expand this script so that it accounts for keyboard interaction with buttons as well as pointer interaction.

avatar image ekergraphics domussolisortum · Mar 22, 2017 at 07:58 AM 0
Share

Looking into it, I believe the correct way to do it is:

txt.CrossFadeColor(btn.colors.highlightedColor, btn.colors.fadeDuration, true, true);

I believe all your multiplication of colors is unnecessary, since this makes it 100% controlled through the Inspector.

avatar image TitusMachine_ · Jun 26, 2018 at 07:25 PM 0
Share

It works very well but not for keyboard/controller input. I'm really looking forward on you figuring out another optimized script with more input support :).

avatar image
5

Answer by zeroblackstar · Sep 09, 2015 at 11:25 AM

Right click Canvas, got to UI, add a Text element. On this text element click on add component and under UI select button.

You now have a button script attached to your text element, change the highlight colour in the button script, click Play and bask in the radiant glow of your new highlighting UI text.

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
2

Answer by x4000 · Sep 19, 2017 at 08:56 PM

Building on what @domussolisortum created, and:

  • making it compatible with TextMeshPro (now free)

  • as well as fixing a bug where if you were holding down the mouse button on a button and then moused out of it it would stay in the pressed state

  • as well as allowing for different colors on the text compared to the main button state, since you might be using sprite swaps on the main button for instance.

  • and finally, making it so that the individual states for things aren't scattered all over the place and thus potentially conflicting with one another:

Code:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 using TMPro;
 
 [RequireComponent( typeof( Button ) )]
 public class FullyReactiveButton : MonoBehaviour, IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
 {
     private TextMeshProUGUI txt;
     private Button btn;
 
     public Color normalColor;
     public Color disabledColor;
     public Color pressedColor;
     public Color highlightedColor;
 
     void Start()
     {
         txt = GetComponentInChildren<TextMeshProUGUI>();
         btn = gameObject.GetComponent<Button>();
     }
 
     private ButtonStatus lastButtonStatus = ButtonStatus.Normal;
     private bool isHighlightDesired = false;
     private bool isPressedDesired = false;
 
     void Update()
     {
         ButtonStatus desiredButtonStatus = ButtonStatus.Normal;
         if ( !btn.interactable )
             desiredButtonStatus = ButtonStatus.Disabled;
         else
         {
             if ( isHighlightDesired )
                 desiredButtonStatus = ButtonStatus.Highlighted;
             if ( isPressedDesired )
                 desiredButtonStatus = ButtonStatus.Pressed;
         }
 
         if ( desiredButtonStatus != this.lastButtonStatus )
         {
             this.lastButtonStatus = desiredButtonStatus;
             switch ( this.lastButtonStatus )
             {
                 case ButtonStatus.Normal:
                     txt.color = normalColor;
                     break;
                 case ButtonStatus.Disabled:
                     txt.color = disabledColor;
                     break;
                 case ButtonStatus.Pressed:
                     txt.color = pressedColor;
                     break;
                 case ButtonStatus.Highlighted:
                     txt.color = highlightedColor;
                     break;
             }
         }
     }
 
     public void OnPointerEnter( PointerEventData eventData )
     {
         isHighlightDesired = true;
     }
 
     public void OnPointerDown( PointerEventData eventData )
     {
         isPressedDesired = true;
     }
 
     public void OnPointerUp( PointerEventData eventData )
     {
         isPressedDesired = false;
     }
 
     public void OnPointerExit( PointerEventData eventData )
     {
         isHighlightDesired = false;
     }
 
     public enum ButtonStatus
     {
         Normal,
         Disabled,
         Highlighted,
         Pressed
     }
 }






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
  • 1
  • 2
  • ›

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

36 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

Related Questions

Change text color on GUI.Label 1 Answer

How do I set a GUI button's text using a string from another script? 0 Answers

Gui grid text color changing and mouse hover 0 Answers

guiTexture color half alpha White? 1 Answer

Find GUI Button and Assign Text 1 Answer


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