Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to change Button sprite AND text color?

Discussion in 'UGUI & TextMesh Pro' started by JoeStrout, May 9, 2015.

  1. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    I'm looking into redoing some of the High Frontier UI using the new UI system. But I've run into a snag with buttons. In my old GUIskin, buttons would change both their image and their text color (on both hover and pressed). With the new Buttons, it looks like I can do one or the other, but not both.

    Before I go write my own Button script that allows me to specify both an image and a text color, is there already some off-the-shelf way to do this that I'm missing?

    Thanks,
    - Joe
     
  2. Djfeeler

    Djfeeler

    Joined:
    Oct 11, 2014
    Posts:
    17
    You must access to the gameobject with :

    GameObject object = GameObject.Find("nameObect");

    After you access at this component :

    object.GetComponent<Image>().Sprite = "NameImage"; // Change the Sprite

    If you want change color text :

    object.GetComponent<Text>().text.color = Color.Red;
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    Thank you, but I already knew how to do it in code... the question was whether there was some way to do this via the Button class. The old GUISkin UI does this easily; it seems like an odd oversight for the new UI system.

    Anyway, I decided to work around it by using my own button script in place of Button. Here's what I came up with.
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using UnityEngine.Events;
    4. using UnityEngine.EventSystems;
    5. using System.Collections.Generic;
    6.  
    7. [RequireComponent(typeof(EventTrigger))]
    8. public class SpriteTextButton : MonoBehaviour,
    9. IPointerEnterHandler, IPointerExitHandler, IPointerDownHandler, IPointerUpHandler
    10. {
    11.     #region Public Properties
    12.     public Image targetImage;
    13.     public Text targetText;
    14.  
    15.     public Sprite hoverSprite;
    16.     public Color hoverTextColor;
    17.     public Sprite pressedSprite;
    18.     public Color pressedTextColor;
    19.  
    20.     public UnityEvent OnClick;
    21.     #endregion
    22.     //--------------------------------------------------------------------------------
    23.     #region Private Properties
    24.     Sprite normalSprite;
    25.     Color normalTextColor;
    26.     bool tracking;
    27.     bool inBounds;
    28.     #endregion
    29.     //--------------------------------------------------------------------------------
    30.     #region Interface Methods
    31.     void Start() {
    32.         normalSprite = targetImage.sprite;
    33.         normalTextColor = targetText.color;
    34.     }
    35.  
    36.     public void OnPointerEnter(PointerEventData eventData) {
    37.         inBounds = true;
    38.         UpdateStyle();
    39.     }
    40.  
    41.     public void OnPointerExit(PointerEventData eventData) {
    42.         inBounds = false;
    43.         UpdateStyle();
    44.     }
    45.    
    46.     public void OnPointerDown(PointerEventData eventData) {
    47.         tracking = true;
    48.         inBounds = true;
    49.         UpdateStyle();
    50.     }
    51.    
    52.     public void OnPointerUp(PointerEventData eventData) {
    53.         if (tracking && inBounds && OnClick != null) OnClick.Invoke();
    54.         tracking = false;
    55.         inBounds = false;
    56.         UpdateStyle();
    57.     }
    58.     #endregion
    59.     //--------------------------------------------------------------------------------
    60.     #region Private Methods
    61.     void Set(Sprite sprite, Color textColor) {
    62.         targetImage.sprite = sprite;
    63.         targetText.color = textColor;
    64.     }
    65.     void UpdateStyle() {
    66.         if (!inBounds) {
    67.             Set(normalSprite, normalTextColor);
    68.         } else if (tracking) {
    69.             Set(pressedSprite, pressedTextColor);
    70.         } else {
    71.             Set(hoverSprite, hoverTextColor);
    72.         }
    73.     }
    74.     #endregion
    75. }
    It doesn't do everything the built-in Button class does, but it does everything I care about, and could easily be extended as needed.
     
    Last edited: May 9, 2015
  4. Djfeeler

    Djfeeler

    Joined:
    Oct 11, 2014
    Posts:
    17
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    No, it's not.
     
    RAWilco likes this.