Image next to dynamic Text (centered)

Hi, I found multiple threads regarding this issue, but none of them were properly solved.
Since this is an issue for years I want to dig again. Most of the time it can be solved with TMP inline sprites, but this is not an option in my case.

Here is what I need to achieve:


There is an icon as a text prefix and a text. Both must be centered inside their domain / parent / layout group. Also the text length is dynamic and can change any time @ .

This example works using a setup that should be made for this, but in fact doesn’t:
Yellow = Horizontal Layout Group (all checks off + alignment middle center)
Red = Image
Green = Text with content size fitter (horizontal fit: preferred size)

In theory this is the perfect setup. But Unity already warns:

In the editor when changing the text the layout already falls apart while sometimes adjusting somewhat. In runtime this doesn’t work at all. This is because the yellow layout group doesn’t understand the changes. I would have to disable and enable it via script for it to update to the new dimensions, which is of course not a solution.
Disabling and enabling the checkbox of the layout group also fixes it in the editor for you to test.

Did anyone ever solve this? Remember TMP sprites is not an option.

Greeeeetings from Germany!

Why can’t you use inline graphics / TMP Sprite Assets to display those icons?

Because we can’t include the spritesheets, since the app size can’t get any larger. Other thing is, that our app is skinned, meaning these icons can potentially be different for different skins. These skins are loaded via remote bundles (can’t be included in build).

Font Assets and Sprite Assets can be loaded via bundles. This would enable you to access these icons from either font assets or sprite assets. Since these icons would be part of the text, it would also simplify your layout issue.

Oh really? I was pretty certain, that this is not possible. I evaluated it months ago and TMP seems to exclusively load their sprite assets from the Resource folder. Even the TMP Settings back that:
7202668--864934--TMP Sprite Settings.png
As you can see you have no choice but put your TMP_Sprite Assets into the Resources/ folder. The documentation also says:

How would they be able to load a TMP_Sprite Asset if its in a bundle somewhere? Remember, I just type
<sprite=“common” name=“search”> as a string into my text.
I still would be interested in that, since this could potentially free some space from existing sprite assets that we are currently using!
How would one go about it?

Fix for the original question:
Okay, meanwhile me and my colleague figured a way out to solve the problem above (hold on to your pepperonis everybody, because this works 100%):

The Result:
7202668--864940--Layout Mess.png
Yellow = Horizontal Layout Group
Blue = Empty
Red = Image (right aligned on that empty)
Green = Text

The trick is to add a Layout Element component to each the blue empty and the green text.
Now check “Flexible Width” and set it to a high value (I used 999).
Additionally you have to set a “Min Width” as-well for the blue thing that represents your image width. Otherwise the empty will be just squashed to death.

This is your hierarchy for this (you can set the Horizontal Layout to a fitting size inside your button)
7202668--864931--Bildschirmfoto 2021-06-03 um 11.21.18.png
Setup your Horizontal Layout Group like this:
7202668--864937--Bildschirmfoto 2021-06-03 um 11.30.12.png

Enjoy.

9 Likes

Does this work with sprites instead of images? It looks like the sprite doesnt move like the text.

Nevermind, just use Rect Transform component.

Yeah this works if you have fixed-structured images and texts. But how to insert random images into random text at random place?

Old thread, but I had this same requirements and this more simpler setup works as well:

Parent with Horizontal Layout Group, Child Alignment to Center and Control Child Size to Width:

Child Icon with Layout Element and Min Width set to the same as the height of your icon:

Child TMP Text with Auto Size enabled:

Works perfectly!

1 Like