How to resize a TextureRect correctly in Containers

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By toxicvgl

I put an empty TextureRect in an empty HBoxContainer. Gave it a 64*64 sprite, then the height of the HBoxContainer became 64 automatically. I set the Scale of the TextureRect smaller (0.5*0.5) since it felt too big, but this time the height of the Container did not change (stayed at 64). Moreover, the height cannot be fixed manually in the editor.

I guess it is because the Size of the TextureRect is still 64*64. However, this Size cannot be changed in the editor either (as far as I know).

What is the best way to resize the TextureRect? Is it inevitable to resize the original sprite outside Godot?

:bust_in_silhouette: Reply From: AlexTheRegent

BoxContainer controls size of its childrens. This is why you can’t directly manipulate with size of TextureRect inside box container. To resize TextureRect inside box container, you need to do next things with your TextureRect:

  1. Turn on expand checkbox in TextureRect section. Now your sprite will disappear (note that it’s size became 0). This checkbox allows your textures to be lesser than basic size.
  2. Set desired size using Min Size property (x for width, y for height) in Rect section. After this step your sprite might get stretched. If so, do third step.
  3. Disable fill flag for required orientation(s) in Size Flags section.

Thank you so much for your detailed answer! It saved me a lot of time.
Turning on expand makes the size.x of the texture be 0. Could you tell me why it does not become the full rectangle of the Container? (For me it is what “expand” should does.)

toxicvgl | 2022-10-09 13:50

Current name of the property is quite misleading, this is probably why it got renamed to “Ignore Texture Size” in Godot 4. According to docs, Expand tells engine to “scale texture to fit its bounding rectangle”. So, if your rectangle is smaller than sprite’s resolution, it “fits” it into bounding rectangle, making actual sprite size smaller.

AlexTheRegent | 2022-10-09 18:59

Thank you again. “Ignore Texture Size” sounds more natural for me!

toxicvgl | 2022-10-09 22:27

1 Like