Hello everyone,
Here is the situation. I have around 400 sprites saved on my disk and I pack them in several 4096x4096 textures, or a spritesheet if you will. This works fine.
The spritesheets are .png files compressed using pngquant and are each around 700 Kb, which is fine.
The problem is that when I load the spritesheets and recreate the 400 sprites using Sprite.create, then the amount of memory used quickly goes beyond 1 Go of RAM as the .png files are decompressed. This isn’t a problem on the machine I’m using but could really be one on other lower-end machines.
My question is: is there a way to keep the memory used as low as possible? By compressing the sprites differently maybe? Or using a completely different approach?
Thanks in advance.
Is there a reason you need to load all 400 sprites at the same time? It would be wise to pack them into sheets of sprites that are most often used together to reduce the number of spritesheets required per scene, and then only load those as necessary, unloading all others.
Unity does this built-in using PackingTags and the SpritePacker. That way when you run a scene, only the sprite sheets that are referenced within the scene get loaded, and hopefully with the least number of sprite sheets possible if sprites were packed using good packing tag grouping.
1 Like
Basically, the game I’m making is a 2D fighting game where the players have to record themselves via a webcam and then, through some processing steps, they will appear in the game. So the 400 sprites are nothing more than the animations of all the moves the players have recorded.
So yes, I have to load all the sprites because if I don’t and a player decides to use a movement that isn’t loaded, the game will stop until the PC finishes loading all the sprites.
I see, well if there are 400 sprites worth of animations available at any given time, then as far as I know the only way to reduce that is via compression and/or sprite resolution, or to reduce the recorded animation sample rate to reduce the number of sprites. Otherwise that high usage of ram may simply be a minimum system requirement to play your game. I am no expert on that sort of optimization though, so maybe someone else has a solution.
Looking at sprite.create I can’t tell if it’s using the texture you pass in or duplicating the memory. I am thinking it should be keeping the texture and not increasing the memory.
4096x4096x4 = 67,108,864bytes. Or about 67mb.
If sprite is reusing the 4096 textures you must have a lot of textures to get to 1gb.
The size of the png has no relation to the size in memory.
Png is compressed and once you load it into memory it’s uncompressed.
If you tell unity to generate mips then each texture will increase from 67mb to about 87mb.
So if you have 15 textures that’s put you in the 1gb range.
The only ways to do better are
Reduce number of textures
Lower color space from 32bit
Compress them(gpu supported compression)
Pack them tighter
Not load them all at once
Lower resolution
1 Like