Search Unity

UI Text as Button

Discussion in 'UGUI & TextMesh Pro' started by Winklehner, Aug 21, 2014.

  1. Winklehner

    Winklehner

    Joined:
    Apr 12, 2013
    Posts:
    20
    Hi,

    I'm playing around with the new UI and was wondering, if there is a possibility/script to make a text element behave like a button, or do i have to give the button a transparent image?
    Furthermore the ColorTint button transition is great but it would be even better if there would be some way to apply it also to the attached text!?
     
  2. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    If you have a game object with your Text component, just add a Button component. A button doesn't rely on an image. The Color Tint will automatically use the Text for the transitions.
     
    shrinath-kopare likes this.
  3. Winklehner

    Winklehner

    Joined:
    Apr 12, 2013
    Posts:
    20
    Thats neat! Still the question remains if I add a standard button, that has an image, the color tint is only applied to the image and not the button text...
     
  4. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    Drag the Text game object into the Graphics Target slot of the Button.

    If you want to affect more than one object, you may use an animation. You need to switch the Transition from ColorTint to Animation in that case.
     
    Arkadi097 and shrinath-kopare like this.
  5. Breyer

    Breyer

    Joined:
    Nov 10, 2012
    Posts:
    412
    it would be awesome if these button GO (parented to text GO) could affect text inside button rect only (instead of affect all text) - this would solve all problem with rich text (event firing included) because GO allow have multiple childrens. It would be way clearer than string rich text i think. of course it would need a bit different workflow and maybe new button type. However its impossible right now until someone create custom open source uGUI or Unity implement this
     
  6. ortin

    ortin

    Joined:
    Jan 13, 2013
    Posts:
    221
    Much better/flexible solution would be to have an array of Graphic as a target of Tint transition.
     
  7. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    It is not a big deal to create such a component on your own.
     
  8. ortin

    ortin

    Joined:
    Jan 13, 2013
    Posts:
    221
    Not sure what you mean: create a custom Button script with array for Tint transition?
     
  9. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,657
    Yes. You could even try deriving your script from the built-in Button and see if you can override just the parts that deal with doing color changes...
     
  10. ortin

    ortin

    Joined:
    Jan 13, 2013
    Posts:
    221
    Well, of course I can do it, and probably will (or may be will write some component which propagates color to its children), but using an array works the same like now, but just gives additional flexibility (imagine you have button with text and icon and you want them both to be tinted on press/hover/etc or some complex button background which consists of few elements).
     
  11. Dantus

    Dantus

    Joined:
    Oct 21, 2009
    Posts:
    5,667
    I understand that this is more flexible, but it is still understandable to me why there are just a few options. If you need total flexibility, you can use the animation component. As soon as you look for something that is easy to set up and is supposed to be flexible, it will work in some cases and in others it won't. You may e.g. say that and array of graphic targets would be sufficient, that's fine. Then someone else thinks, why can't I have an array of such transitions, but each of them needs an array of graphics targets, such that a group of objects gets the same colors for the transition, but another group has its own colors.
    I really see your point, but I believe it makes a lot of sense like that.