Search code examples
iossprite-kittexturessprite

Correct way to import and create many large sprites: SpriteKit


Despite reading through much of Apple's docs and reading through their forums and watching their WWDC videos, I seem to have missed the steps required to utilise the texture packing and performance of Sprite Kit for making many large-ish Sprites.

Imagine 100 different sprite images, for 100 different sprites. If each image is 256x256 pixels, they're going to take up more than one SpriteSheet at 2048x2048. And let's imagine that's the limit, rather than 4096x4096, just so it's understood that more than one spritesheet is going to be required.

There might be as many as 20 different sprites on screen at any given time. So performance is a consideration.

How to import these images and then create these sprites in the manner SpriteKit intends for its texture packing and performance considerations of in game use?

Just a specific link to Apple recommended absolute steps will be fine. I must have just skimmed right over it.

Apple's new docs go this far:

enter image description here

Which completely fails to convey:

  1. Import folder, or images?
  2. Import to WHERE within the project?
  3. Load and reference them, how?

Solution

  • So let me see if I can reasonably answer your question. Please keep in mind that while I have used SpriteKit, I'm not a big fan of it. And when I did use it, I did not use any of the tools Apple provided (eg. SKTextureAtlas, sks files, etc). Nevertheless, this should still be applicable. Best practices as per Apple? I have no idea.

    Question: Import folder, or images?

    If you use Xcode's means of generating atlases, you Add Files... to your project a folder named XYZ.atlas, where XYZ will be the texture name. This folder contains all of your textures which will be in the atlas.

    Question: Import to WHERE within the project?

    Just has to be in your project. But you should have some organization Group hierarchy in your project file.

    Question: Load and reference them, how?

    An example of loading would be something like (yeah, I know boo, Obj-C):

    self.myTextureAtlas = [SKTextureAtlas atlasNamed:@"MyTexture"];
    

    Inevitably, you will want access to the actual textures and you'll need to do something like:

    self.tex0 = [self.myTextureAtlas textureNamed:@"tex0"];
    

    A good tutorial is here: https://www.raywenderlich.com/45152/sprite-kit-tutorial-animations-and-texture-atlases

    Here is also a screenshot that shows the .atlas folder. It also shows a couple of tool generated atlas files.

    enter image description here

    So here you can see I have 2 Groups which are references to folder MyTexture.atlas and ProgressBar.atlas. In game, they will be called MyTexture and ProgressBar.

    I have also included, just as an example the same atlases, but pre-built. You would not have both in your project, I only used it to show what including a pre-built would loo like. I generated them using TexturePacker. I'll get into why this may be a better option later on. TexturePacker also can create SpriteKit atlases instead of the atlas PNG.

    In reality, an atlas is really just a texture with sub-textures are associated with it. The sub-textures are X/Y/W/H sections of the texture. The actual memory for the texture is "held" by the atlas. Understanding what an atlas is is a useful thing, because it allows you to think through how you would support it if you had to implement it yourself. Or enhance it.

    Now let's go over how you would use this altogether to do something useful.And for this you're going to need a texture manager (aka TextureManager), sprite manager (aka SpriteManager) and a manifest of some sort.

    The manifest is really some form of association between "sprite name" to atlas:sub texture pair. For example:

     {
      "progressbar": {
        "atlas": "ProgressBar",
        "subtexture": progressbarbacking"
      },
      "progressbarfillr": {
        "atlas": "ProgressBar",
        "subtexture": progressbarfillr"
      }
    }
    

    In this case it is some JSON, but you can have whatever format you want. When I build my games, I have a build assets phase which generates all my textures and from that, builds a manifest. This manifest tells me not only what textures exist, but is used later on to find the correct association of a "sprite name" to the actual atlas and sub texture. "sprite name" is just some name you have associated meaning. It could be "zombie" for example.

    You use a TextureManager as your asynchronous loader. In addition, it is your inventory manager of all your textures. As an inventory manager, it will prevent you from double loading textures and also give you the correct reference to textures/atlases when requested (if they exist).

    You would use the SpriteManager to create a new SKSpriteNode using the data from the manifest. For example:

    SKSpriteNode *progressBar = [[SpriteManager sharedInstance] createSprite:@"progressbar"];
    

    Here I've made it a singleton. Use whatever implementation you want. If you hate singletons, that is fine, this is just an example. You'll note that it returns a SKSpriteNode. I see a lot of people making subclasses from SKSpriteNodes. I never do this. To me, the graphic component is always a "has a". However, if you are doing an "is a", you can still do this. You just need to feed in the class you need. I'm considering the way of handling that out of scope for this question.

    Now if you look at the manifest, you'll notice that progressbar is associated with an atlas named ProgressBar and a sub texture named progressbarbacking. To get the texture you need, you'd have some implementation code in SpriteManager like:

    // NOTE the literal names would be variables which contained the unpacked association from progressbar
    // literal strings are used to make the idea easier to follow
    SKTextureAtlas *atlas = [[TextureMaanger sharedInstance] findAtlasNamed:@"ProgressBar"];
    //Error check of course
    
    SKTexture *tex = [atlas textureNamed:@"progressbarbacking"];
    // Error check of course
    
    SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithTexture:tex];
    

    Or perhaps you would just have a call:

    SKTexture *tex = [[TextureManager] sharedInstance] texNamed:@"progressbarbacking" atlas:@"ProgressBar"];
    SKSpriteNode *sprite = [SKSpriteNode spriteNodeWithTexture:tex];
    

    And there you have it. A means to get sprites from atlases.

    Best practices as per Apple? Dunno, but it is along the lines of what I do.

    Some people will complain that there will be dictionaries involved and some of this will be "slow". And yes, there is a penalty for this. And reality, I don't use a dictionary for this either, but it is easier to get the idea across as the dictionary. Also keep in mind that I consider the usage of this to occur during loading phases and very little during game play, if at all. One of the tricks to performant games is pre-loading all or as much of the data you need prior to actual game play.

    Oh, going to why I pre-build the atlases. So part of your question was organization of textures to atlas. And that is why. I like to see the generated atlas and understand what the size is and what is in it. Additionally it makes downloadable atlases easier.

    As an optimization, you would want to try and put textures in which are all drawn relatively the same time. For example, it would make sense to have all HUD items in the same atlas versus mixing HUD with background.