Get a subimage from sprite sheet

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

Hello,

I’m trying to implement a system where the player can have custom animations, and it’s supplied to the engine through a folder in the user:// directory as a sprite sheet. I’m trying to make a preview icon next to the player select that only displays the first frame of an animation. The preview icon is a TextureRect.

In order for it to work I need to be able to cut the image programatically, however, three hours of google searching led me to nothing, so I ask: is there a way to do this in GDScript?

Thanks in advance

:bust_in_silhouette: Reply From: Friends-Games

You can do this and it works:
First, change the TextureRect to a sprite. Turn on the region and set it to the size of your Sprite in the sprite sheet.

$Sprite.rect(0,0,20,20)

Call the Sprite, get the “rect” property, and set the first 2 numbers to the position where you want it. Then, the last 2 numbers are the size of the rect.
This is a simple way of doing this.

I like your answer, and I think this would work for sure. However, because my UI is supposed to be dynamic, like when the screen size and/or resolution changes, the UI is supposed to scale accordingly. I’m currently looking into AtlasTextures to see if I am able to do what I wish to do using that, if you have any thoughts on how that might work.

Surge | 2020-07-16 02:19