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

Simple text problem, no simple solution

Discussion in 'UGUI & TextMesh Pro' started by mathiassoeholm, Aug 22, 2014.

  1. mathiassoeholm

    mathiassoeholm

    Joined:
    Jul 20, 2010
    Posts:
    104
    Hey there!

    What I'm trying to achieve, is a simple button with a price and a currency icon.
    I need the text and icon to be centred on the button.

    This is however not as easy as it sounds.
    For me to anchor the text to the currency icon, or vice-versa, I need the text to be aligned on the side.
    Now how do I centre the text on the button, while the text is aligned to the side?

    I will need to know the size of the text itself, and not just the bounding box.

    I've had a similar problem using NGUI.
    Here I would just use a label with the ReesizeFreely option on, this way the text will always fit the size of the bounding box. And I can use centre alignment and anchor the currency icon on the side.
    Only downside to this solution, is of course that a large enough number will exceed the boundaries of the button.

    I think this is a pretty common UI problem, so I would like to hear people's approach.

    Cheers :D

     
    rakkarage likes this.
  2. Chri907

    Chri907

    Joined:
    Jun 25, 2013
    Posts:
    5
    Also I have had a similar problem in past but I was unable to solve (with ngui) ...
    I would like to know it me too how to make, if possible :)

    (sorry for my english)
     
  3. rakkarage

    rakkarage

    Joined:
    Feb 3, 2014
    Posts:
    683
    drag that blue ring (pivot) from middle of text to middle of button?
     
  4. dakka

    dakka

    Joined:
    Jun 25, 2010
    Posts:
    113
    It would be good if rich text supported sprites :) then it would just auto center.
     
  5. Melang

    Melang

    Joined:
    Mar 30, 2014
    Posts:
    166
    This is a bit tricky...
    1) Right-click on the button --> "Create Empty". The new GameObject will be automatically positioned and aligned to the center of your button
    2) To the new object, add components Layout - Horizontal Layout Group and Layout - Content Size Fitter, for the latter choose "Horizontal Fit - PreferredSize"
    3) parent both icon and button text to this new object
     

    Attached Files:

  6. mathiassoeholm

    mathiassoeholm

    Joined:
    Jul 20, 2010
    Posts:
    104
    Looks great Melang!

    Will give it a try :)
     
    Tim-C likes this.
  7. Chri907

    Chri907

    Joined:
    Jun 25, 2013
    Posts:
    5
    Great, i have succeeded!
    Thank you very much
     
    davitsedrakian, runevision and Tim-C like this.
  8. mathiassoeholm

    mathiassoeholm

    Joined:
    Jul 20, 2010
    Posts:
    104
    Works like a charm!
     
  9. SweatyChair

    SweatyChair

    Joined:
    Feb 15, 2016
    Posts:
    140
    looked for same solution for currency shop

    In my case I also need to add a LayoutElement to the icon image with preferred width, and tick control child size in HorizontalLayoutGroup




     
  10. davitsedrakian

    davitsedrakian

    Joined:
    Jun 23, 2018
    Posts:
    30
    This works! :) Thanks
     
    snyup likes this.