Build Sprite Sheet At Runtime

Hey folks. I am building a 2D game where each level the player drives a train from station to station and fights baddies on it on the way. Between any given station, the player can completely change their train’s composition, from a big selection of carriages, weapons, cargo, etc.

I want to take advantage of the performance optimization of sprite sheets, but I really don’t need all the possible sprites the train COULD have loaded into memory for each level - only the ones that are currently being used (since they can’t change their loadout during the level). Since the loadout is up to the player, I can’t make the sprite atlas at build time.

I was hoping I could programatically generate a sprite sheet for each level while the level is loading, based on the player’s current loadout. However, everything I can find about Unity’s sprite packer (Unity - Manual: Sprite Packer) seems to be targeted at build time rather than run time. Could anyone point me in the right direction for dynamically creating sprite sheets at run time?

The only way I can think about doing this is the following:

On the scene where you let the user chose the train parts you have all loaded right?

So you will need to know what he chose and use ReadPixels to get the image you want from whatever atlas its in.

Then, once you got all the train parts as pixels, you create a new texture and use SetPixels
to basically create a new atlas (you will have to think of an algorithm to make sure they are spaced etc)

Once you have the new texture with all the selected train parts, you can use Sprite.Create to create sprites at runtime.

This whole process is pretty heavy I think, so it might be better to just stick to what you got.

This is the only way I can think about doing this at runtime.