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.

sheets-from-folder.sh
#!/bin/bash

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

# Root folder
ROOT_FOLDER=$1

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

# 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"
    fi
done

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 sheets-from-folder.sh

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

./sheets-from-folder.sh assets

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