Search Unity

Unity UI Right justify text but keep it centered?

Discussion in 'UGUI & TextMesh Pro' started by Denisowator, Mar 2, 2019.

  1. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Let's say I have a text field that has "100/100" as its content.

    Currently the text object is stretched across a parent, to make sure it's always centered.
    upload_2019-3-2_18-17-56.png

    I also have the text center justified and center aligned vertically:
    upload_2019-3-2_18-29-34.png

    If the text content goes down to "99/100", or even to "9/100", both the left and right sides will shift inwards, because the text is center justified.

    How can I keep the overall text in the center of whatever its parent happens to be, but have the text right justified so if the content length decreases or increases, only the left side will shift?
     
    Last edited: Mar 2, 2019
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Did you try content size fitter on the textelement? That way you could try to set a max width with anchors and have it right
     
  3. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Yes, but I don't know what values to use for the anchors, and the fitter settings.
     
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Preferred width on the fitter. And on the anchors, you could change the max and min x y to what ever percentage you want, for example, min X 0.1 and max X 0.9, this way you stay dynamic and still have a max width for your text.
     
  5. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Still behaves the same.

    upload_2019-3-6_20-50-49.png
    upload_2019-3-6_20-51-51.png
     
  6. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Can you show a screenshot of your ui in scene or gameview?
     
  7. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
  8. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    How do you know it is not justified right? Its in the center and only one line.
     
  9. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    It is justified right, but it's acting like it's not. Possibly because the content size fitter is always keeping the text area the width of the contents.