Move button according to the text size

Hey
How can I keep the button position fixed to the text (even if it changes)?

Thank you in advance

Both Unity’s default GUI Text objects and the now-free TextMesh Pro GUI Text objects have a property called “preferredWidth”. If you use that, you can size your text-box to its appropriate size every time you update your score (use its RectTransform.sizeDelta property for resizing). Therefore the text will always fit in the text box, with no overflow.

To have the button position relative to the score text box, simply move the button object underneath the score in your hierarchy view, so that it’s a child of the score. Then, position it how you want it, and when the score resizes, your button should move with it.

Note, the first paragraph is important! Even though your text box might look okay with the text overflowing, the rest of the UI system uses the text’s RectTransform’s for positional information. You must resize the text for any children to update their positions.

Objects are parented, but I don’t know what you mean by “preferredWidth”

Perhaps he meant ‘BestFit’ for Text. preferredWidth is a ContentSizeFitter or LayoutElement thing, I think :slight_smile: And the previous poster mentioned making the button a child of the text, this way it’s layout offset will stay up to-date. Layouts can be awesome & tricky lol
Does any of that yield some good results?

Not really, I tried both ContentSizeFitter and ‘BestFit’ and had no luck.

Could it be because I’m changing the score value through script?

Okay, I have 2 suggestions for you :slight_smile:
1 is to download the TextMeshPro package. It’s free, new, soon to be completely integrated into Unity (not yet , though, you have to get it in the asset store). You can add sprites into your text there, easily.

Now, to the solution with the UI system.
I put a Text element on the screen. I add a content size fitter and enable horizontal preferred size (vertical’s probably good to add, also).
Then, I add the image as a child of the Text element. I set it’s anchors and pivot using one of the easy presets to the right-middle combination. I adjust the image’s offset to about +15 in the x position.
And that’s working for me.

1 Like

Text elements have properties called preferredWidth and preferredHeight. I just looked them up in the Unity documentation and almost no detail is given on them – as methos5k mentioned, I think the intent is to use them with a LayoutElement.

But you can use them manually if that’s easier for you. For example, I Instantiate() Text elements in my game, which I don’t think the layout system supports, so I manually resize my Text elements to organize my UI around them. Here’s an example from my code:

summaryText.rectTransform.sizeDelta = new Vector2(summaryText.rectTransform.sizeDelta.x, summaryText.preferredHeight);

As you can see, I resize my summaryText element by adjusting its RectTransform’s sizeDelta property. The vertical part of that is controlled by summaryText.preferredHeight. (Note: I actually don’t use preferredWidth anywhere in my code, but I assume it performs similar functions, though I could be wrong.)

TextMeshPro elements also have a GetPreferredValues() function that will return a Vector2 of width and height.

1 Like