Can the Sprite Kit load a texture atlas several times if later using SKTextureAtlas?

It makes me think.

The game starts and I create sprites using the -spriteNodeWithImageNamed: method. Later for animation, I create an SKTextureAtlas object. Some people say this is faster because -spriteNodeWithImageNamed: first look in your application package for png, and then it will look at the atlas.

But what is not clear to me: if I create SKTextureAtlas later, will it know about the atlas image already loaded, or will it be dumb and just load the image again?

And if I create an SKTextureAtlas object in several nodes for the same atlas. Will he download the atlas several times? Should I make sure that only one SKTextureAtlas instance is used for any atlas?

+2
source share
1 answer

It is true that spriteNodeWithImageNamed: will search for the file in the package first. If he cannot find the package file, he checks if an image with the same name exists in the atlas available in the bundle.

If the Sprite Kit finds an image with this name in any atlas, it will automatically download this atlas to use the specified image as a sprite when using the spriteNode/initWithImageNamed: initializers. This makes it easy to start development without atlases, and then add files to the atlas.

I recommend using atlases from the very beginning, because there can be subtle differences, and you can more effectively evaluate the performance of your application.

Yes, the Sprite Kit is smart enough not to reload a resource that is already in memory. It also will not create a new instance of one atlas, but rather will return you a pointer to an existing atlas with the same name.

Sprite Kit also uses a caching mechanism, so even if the last strong link to the resource file was deleted, the file will remain in memory. However, I don’t think anyone has done a detailed analysis of how and when and in what order the Sprite Kit ultimately releases cached instances from memory.

In short: rely on the Sprite Kit to do the right thing for you.

+6
source

All Articles