Programatically generate an AtlasTexture given an Array of Sprites/Texture2D

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

I’m trying to generate an AtlasTexture programatically given a collection of .png resources found in my project.

The idea behind it is to create a .json with the id and paths to these resources, then for each entry in it load the textures into an AtlasTexture, with the id being the position of the texture in the atlas.

The size of the atlas needs to also be dynamic, depending on the number of entries, so if we have 255 textures of 32px each, ceil( 255 / 2 ) * 32 = 4096px width/height.

How would I do this?

:bust_in_silhouette: Reply From: jgodfrey

Seems like you can fit 256, 32px images in a 512x512 image…

Regardless, you can create a single, larger image from a number of smaller images using the Image object and its various methods. Specifically, you’ll likely need:

Image.create() - create an empty image of the final size
Image.load() - load in the individual, smaller images
Image.blit_rect() - add each individual image, at its appropriate “cell” location, to the larger image

Docs here.

Thanks, that is precisely what I was looking for.

MEDBVLL | 2023-05-17 18:58