How to create Sprite Sheets from multiple Folders

Andreas Löw
Last updated:

Let's assume you have a main folder that contains sub folders in which you keep the sprites for your game.

E.g. like this:

  • 📁 assets
    • 📁 foreground
      • grass.png
      • tree.png
      • barrel.png
    • 📁 character
      • walk-01.png
      • walk-02.png

You want to create a sprite sheet for each of these folders.

The easiest way to do this is to use TexturePacker.You can use the MultiPack Mode for this — but this requires a bit of configuration in the graphical user interface.

Another option is to use a bash script that scans the root folder for folders and automatically packs a sheet for each of them.

For this, install TexturePacker's command line client and save this as a bash script.

# Check if the folder name argument is provided
if [ "$#" -ne 1 ]; then
    echo "Usage: $0 root_folder_name"
    exit 1

# Root folder

# Check if the root folder exists
if [ ! -d "$ROOT_FOLDER" ]; then
    echo "Root folder '$ROOT_FOLDER' does not exist!"
    exit 1

# Iterate over immediate subdirectories
for SUBDIR in "$ROOT_FOLDER"/*; do
    if [ -d "$SUBDIR" ]; then
        BASENAME=$(basename "$SUBDIR")
        echo "Processing $SUBDIR ..."
        TexturePacker --sheet "${BASENAME}.png" --data "${BASENAME}.json" "$SUBDIR"

echo "Done"

Adjust the highlighted line to contain the parameters you want to use for the sprite sheet creation. See the Texture Settings in the documentation for more details.

Give the script execution rights:

chmod u+x

Assuming, your files are all in the assets folder, create and update your sprite sheets using this command:

./ assets

TexturePacker checks for changes in the folders and only update the sprite sheets when files were added, removed or changed.