mredig
mredig

Reputation: 1805

Using an SKTextureAtlas instead of just calling the image by name

Why would I want to do one of these options over the other... I guess, I don't understand how they differ beyond what I have to code

-make a texture atlas named "HeroSprites.atlas" in Xcode with the image "Hero.png" and possibly other variations in it.

Example 1:

SKSpriteNode* heroSprite = [SKSpriteNode spriteNodeWithImageNamed:@"Hero"];
//pulls in image from the atlas without having created an atlas object first
[self addChild: heroSprite];

Example 2:

SKTextureAtlas* heroAtlas = [SKTextureAtlas atlasNamed:@"HeroAtlas"];
SKSpriteNode* heroSprite = [SKSpriteNode spriteNodeWithTexture:[heroAtlas textureNamed:@"Hero"]];
//pulls in image from the atlas after having created an atlas object first
[self addChild:heroSprite];

As far as I can tell, both seem to load the atlas into memory so that I can call upon all the images within. The only reason I can think for this is if there were identical image names in separate atlases, but it would be easy enough to avoid that.

Upvotes: 0

Views: 1735

Answers (2)

geowar
geowar

Reputation: 4447

A SKTextureAtlas can improve memory usage and rendering performance. For example, if you have a scene with sprites drawn with different textures, Sprite Kit performs one drawing pass for each texture. However, if all of the textures were loaded from the same texture atlas, then Sprite Kit can render the sprites in a single drawing pass—and use less memory to do so. Whenever you have textures that are always used together, you should store them in an atlas.

Upvotes: 1

Theis Egeberg
Theis Egeberg

Reputation: 2576

Here is what you should know about SKTextureAtlas:

  • As long as you save the files directly into the atlas folder in your resource tree you don't have to add it manually to file directory.
  • Drawing is faster since all sprites with same blend mode and textures from same atlas are drawn in one go.
  • Does not support mipmaps (and this is sadly not documented by Apple).
  • Shares the blend mode among all of its textures (this can be work-arounded by having two atlas', one for linear and one for nearest).

Upvotes: 1

Related Questions