问题
I have a lot of characters in my game and because of that I have so many textures. When a texture atlas is loaded (containing about 5 different image textures) it increases the memory use and keeps it there at that amount. So the more textures just keeps driving that number up and up until sometimes the application crashes. I don't need all the characters at once, how can i maybe load some character textures when I need them and deallocate the others when i don't, but ill need to be able to bring it back.
回答1:
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.
来源:https://stackoverflow.com/questions/37825039/swift-how-to-handle-a-lot-of-textures-in-memory