How can I show only part of a texture in a TextureRect (or other control node)

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

I have all my icons packed in a single texture, which is great when I’m using them in sprites, but now I want to use them in my UI - I’d like to position several icons next to each other using an HBoxContainer.

My problem is that I can’t work out how to only display ONE of my icons from the texture using a TextureRect - I can only display the whole texture (using stretch mode: tile, I can show only part of the texture, but it’s always the top-left part)

I tried using sprites instead, because I can provide a UV offset with those, but I couldn’t get the HBoxContainer to control their positions. My options seem to be to divide the icons into individual textures, or maybe write a shader, but I’m hoping I’m missing something obvious.

Is there any way to show a specified part of a texture using a TextureRect or other control node?

:bust_in_silhouette: Reply From: Zylann

TextureRect indeed doesn’t have a region property like Sprite, so you may have to create an AtlasTexture instead: AtlasTexture — Godot Engine (stable) documentation in English

Note: the description is misleading. AtlasTexture doesn’t “contains” multiple textures, it actually references a sub-region of a larger texture instead. They are lightweight and aren’t copying pixels.

Thank you! (I’d resigned myself to using sprites and managing their positions myself, which would have been a shame)

Ben Rolfe | 2020-03-09 11:12

Should be renamed to TextureRegion.

luislodosm | 2022-09-25 19:30

1 Like