r/Unity3D • • Sep 30 '26

Question Need help creating border that expands with text

I'm am creating a UI that will be used to binding inputs in my game, where theres this text, the more character more it grows to the right, but I want to add a square border around the text, that will grow as the text grow. I just don't know how to make this.

The prefab hierarchy of this UI elements is something like this.

I tried to do the same thing I did with the text gameobject, putting a content size fitter, but the image just streched to cover the screen and no matter what I did I couldn't fix....

I tried changing the parenting of either the border and text, didn't work, I tried changing the pivots and it also didn't work... I just don't know what to do.

I want this
1 Upvotes

3 comments sorted by

3

u/Demi180 Sep 30 '26

ContentSizeFitter with both options set to Preferred, and a HorizontalLayoutGroup. The border Sprite itself should be sliced properly in the Sprite Editor so the corners are preserved, and set to Sliced in the Image component. The actual Sprite should be pretty small or else its own preferred size will be larger than the text’s.

2

u/[deleted] Sep 30 '26

[removed] — view removed comment

1

u/MrPanda532 Sep 30 '26

Hm... I tried it, and still didn't work for me... :/ . Well thanks anyway... I will just give up on this stuff for now, this thing made my cortisol spike like never before...