Unity3D: 12 Ways to Save Texture Memory in 2D Games

When developing a 2D game, it can quickly hit the roof in term of memory usage, especially when targeting mobile devices. A game using too much memory will drain the battery down or simply crash if it exceeds the maximum allowed by the OS. Here are a few basic tricks worth mastering when you first start creating or optimizing 2D games in Unity. Make sure to go over these tips with artists as well because a lot of these are related to their work.

1 - Use atlases. Place Sprites that are related in the same atlas. Unity will create a single Texture with all these Sprites together which optimizes memory usage in a completely transparent way to developer. Documentation: https://docs.unity3d.com/Manual/SpriteAtlas.html

2 - Use tiles. For instance, if you have a grass background for a top-down view game, instead of having a huge grass texture to cover the whole screen, have a small tileable Sprite that will repeat itself all over the scene. Documentation: https://docs.unity3d.com/ScriptReference/SpriteDrawMode.html


3 - Use the 9-slice technic to create Sprites that stretch their center rectangle to any size as in the example below. Documentation: https://docs.unity3d.com/Manual/9SliceSprites.html



4 - If you have a Sprite that comes in multiple color variations, instead of exporting multiple versions of the same Sprite, export a single one in grayscale and use the Color property of the SpriteRenderer to reproduce your color variations. Note that you might have to divide and export your object in multiple Sprites to color the different parts of the object independently.

5 - Stretch a small 1x1 white pixel Sprite to reproduce plain color rectangles in your scene instead of having a huge Sprite that's only a plain colored rectangle. Use the Color property of the SpriteRenderer to color rectangles created that way.

6 - Use Unity's basic vector sprite shapes. Shapes available are squares, triangles, diamonds, hexagons, circles and polygons. They are extremely lightweight memory wise and can be stretched to any size since they are vector graphics.

7 - Load Sprites dynamically from Resources for things you have a lot of. For example, pictures of animal in a huge catalog of animals. That way, Sprites are only loaded when needed instead of beeing loaded in memory all together at the start of the game. Warning: Don't put textures that are in resources folder in atlases as they will be duplicated in your build. Documentation: https://docs.unity3d.com/ScriptReference/Resources.Load.html


8 - Limit the use of textures bigger than 1024x1024 because they can use up to 10mb or more in memory. Be creative and export your Sprites into smaller parts when possible.


9 - Use Unity Texture compression when it doesn't affect the resulting quality. Test with difference Compression qualities and you can also try to reduce the Max Size and see if your scene becomes too ugly or not.

10 - Export your Sprite in power of two dimensions. Make sure all textures not in an atlas have a dimensions that is a power of two to improve compression results.


11 - When making vertical or horizontal gradient Sprites, make them 1 pixel thin and stretch them as needed on the scene to reproduce the exact same effect at a tiny fraction of the memory cost.


12 - After a build, take a look at the report to get a list of the assets sorted by memory usage. Go down the list and try to cut the fat by using previous technics shown in this blog post!