UMG Image Size to Fit

I’m creating a messages pane in UMG to show player kills and flag captures at the top right of the screen. I have a ScrollBox that I populate with MessageItem widgets that contain <Text><Image><Text> widgets to show ‘John’ <rifle weapon image> ‘Bob’

So far everything is working great but I was hoping to also use the same MessageItem widget to show flag captures so ‘John’ <flag image> ‘Flag 1’

Right now weapon textures are 64x32 and flag textures are 32x32. If I set the default MessageItem Image widget to 64x32 then it works fine for weapon textures but flag textures get stretches to 64 x 32 and appear too wide obviously.

Is there a way to set an Image widget to size itself automatically to fit its content or do I have to achieve this manually? I guess I could either make specific FlagMesageItem and WeaponMessageItem widgets or just detect inside the general message item widget if it is for a flag/weapon and resize the widget manually?

if you have a vertical box, I think you can have a widget blueprint where you can specify the width(or any parameters really), then when you have a new message, just create a widget object and add to that vertical box, and if vertical box have more children(say more than 8), then remove the first one before you add new ones.

PenguinTD thanks for the response but I’m not sure you understand my issue correctly. I am able to populate a scrollbox with my own widgets dynamically just fine.

My issue is that my MessageItem row widget has a <KillerTextWidget><WeaponImageWidget><VictimTextWidget> and I cannot figure out how to allow the WeaponImageWidget to accept different sized textures and adjust its size accordingly.

So say for example a player kills another player with a grenade, the grenade icon is 30x30 so I can set the WeaponImageWidget X to 30 and Y to 30 in UMB. But then if the player is killed by a rifle, the rifle icon is 60x30 and setting WeaponImageWidget at to it at runtime will cause the icon to be squished to 30x30.

Is there a way to tell the ImageWidget to adapt its size based on the size of the image it is displaying at runtime?

Yes, the image widget reports a desired size based on the size of the brush it is displaying. So ensure that the brush you’re creating uses the size of the texture, nodes like CreateBrushFromTexture will automatically fill in the sizes of the brush unless you override them at runtime.

Nick! thanks for the reply!

I searched for a CreateBrushFromTexture node but I do not see one in 4.6.1? Did you mean SetBrushFromTexture?

Right now I drive the ImageWidget with a ‘Set Brush from Texture’ node from a UTexture2D * on a MessageItem struct.

See the following screenshot:

I still have the issue where ImageWidget does not adjust it’s size when setting it’s brush with a UTexture2D. It seems to hold the same X/Y that is set in UMG

I thought this was due to the fact that I am casting a FCanvasIcon’s UTexture to a UTexture2D in C++ to set the UTexture2D * CenterImage on the MessageItem struct.


MessageItem.CenterImage = Cast<UTexture2D>(KillIcon.Texture);

But I changed ShooterGame’s UShooterDamageType KillIcon from FCanvasIcon to UTexture2D and removed the cast but the ImageWidget still does not adapt its size to match.

The node you’re looking for is MakeBrushFromTexture, you’ll need to use it inside a binding for the brush on the image widget, looks like we don’t allow direct assignment of the brush to the image widget.

Ah thank you Nick. That does the trick for UTexture2D’s when the texture is positioned at 0,0. But what about the case when you are working with texture sheets and FCanvasIcon? I notice that FCanvasIcon is not Blueprintable so I cannot break the struct in UMG to get the texture x/y offsets on the texture sheet even if I wanted to… and I don’t think there is a MakeBrushFromTextureWithOffSets node?

How bad of a practice is it to have all of your textures in their own file rather than a texture atlas? That is the preferred workflow for me but I wonder about how inefficient it is? The ShooterGame example uses texture sheets but maybe that is not a necessary practice?

Dunno about FCanvasIcons, the only thing Slate and thus UMG understands is FSlateIcons and UTextures/UMaterials.

Ah ok, thank you. Any thoughts on the texture sheet vs individual texture files?

We don’t have a solution for sprite sheets in UMG yet, the only way to do them currently would be to use your own material. Which would be very time consuming, and may not save you anything.

K I will just use the preferred individual texture per icon solution until it becomes an issue.

Thank you for the response and UMG :slight_smile: