Swift : How to handle a lot of textures in memory

心已入冬 提交于 2019-12-04 15:43:35

Rule 1

First of all you don't need to manually load in memory your texture atlas. When you create a sprite with this code

let sprite = SKSpriteNode(imageNamed: "Dog")

SpriteKit looks for an image named Dog and if it cannot find it then it automatically looks for the image inside all your texture atlases.

When the image is found inside a texture atlas the whole texture atlas is automatically loaded in memory.

This is why you can avoid manually loading the texture atlas.

Rule 2

Don't bother about removing the texture atlas from memory

When you stop using all the images inside a given texture atlas it is automatically removed from memory. This could not happen immediately if the system has "enough" memory but it will happen eventually.

This is why you don't need to manually remove a texture atlas from memory

What can you do?

You should group your textures into several texture atlases following the logic of your game.

If you have 30 textures and you know that only the 1...10 OR 11...20 OR 21...30 will be used at the same time then create 3 texture atlases like follow:

  • TextureAtlas1: images from 1 to 10
  • TextureAtlas2: images from 11 to 20
  • TextureAtlas3: images from 21 to 30

This will make the SpriteKit work more effective.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!