Search code examples
iossprite-kitsktextureatlas

What is the best way to split an SKTextureAtlas for multiple levels in a game?


Currently I have an SKTextureAtlas which is too large to load quickly. It contains textures for all of the enemies in my game. I want to split up the atlas so the game only loads the textures necessary for a certain level. However, some enemies appear in multiple levels. How should I split the atlas up? I would prefer not to create an atlas for each level, because I would have to duplicate the image files for the repeated enemies, and it becomes annoying when there are lots of levels. Is there a more elegant approach?


Solution

  • A good scenario is when all the sprites visibile on the screen for the entire level are into the same Texture Atlas. In this scenario the Texture Atlas is loaded into the GPU and remains there. During each frame the CPU only needs to inform the GPU about the coordinates/positions/transformations for each sprite, saving a lot of data transfer.

    I understand this could force you to duplicate some sprites because some of them are available in multiple levels. It's something only you (as developer of your game) can take.

    From Apple docs

    When you create a texture atlas, you want to strike a balance between collecting too many textures or too few. If you use too few images, SpriteKit may still need many drawing passes to render a frame. If you include too many images, then large amounts of texture data may need to be loaded into memory at once. Because Xcode builds the atlases for you, you can switch between different atlas configurations with relative ease. So experiment with different configurations of your texture atlases and choose the combination that gives you the best performance.