Unity Tips Performance : 2d Sprite Packer
Hello guys, great to see you. Now, we talk about performance in game. Sometimes we must be care about the performance. Because if your game performance good, it means that your game can be played with user or gamer smoothly.
Some game engines usually give some documentation or tips for optimize performance in game. It can optimize about graphic performance, cpu performace, RAM, or something else that can make your game better. In Unity game engine you can optimize your game. In this topic i will discuss about 2d Sprite which may effect in CPU usage or memory. But, first thing we should know about tweaking performance game in unity, you should use profiler in unity. What is Profiler?
Profiler is a tool from unity that helps you to optimize your game. It reports for you how much time is spent in the various areas of your game. For example, it can report the percentage of time spent rendering, animating or in your game logic. You can analyze the performance of the GPU, CPU, memory, rendering, and audio. You can access it from Window > Profiler. You can see the detail from the documentation here.
Let’s started with the package I already provided before. Here is the link for the package sprite packer sample. If you already import into unity, it contains two folder, Scene and sprite.
Each scene has same gameobject with different sprite. Here is the preview of hierarchy
Open first scene. Hit play button, and open the profiler window. Look at Rendering section Process. Under graphic section you can see SetPass Calls or Draw Calls. What is it mean?. I don’t know what exactly mean (LOL), but in unity documentation draw call means “To draw a GameObject on the screen, the engine has to issue a draw call to the graphics API (such as OpenGL or Direct3D). Draw calls are often resource-intensive, with the graphics API doing significant work for every draw call, causing performance overhead on the CPU side”. So, it is better with small draw call. You can see the draw call in first scene is 6.
Open second scene and hit play, observe the profiler like scene one. And then compare the result of profiler from each scene.
You can see the draw call in scene two is 3. Why their draw call different? because sprite setting in each scene different. For the second scene i use sprite packer and first scene don’t use special setting, just default setting. What is sprite packer?? It is a tool from unity that used for pack the graphic in an atlas or single texture (you can see manual here : Sprite Packer). But before use sprite packer, add tag for each sprite in import setting.
After tagging each sprite, click window -> sprite packer
Then sprite packer window will showed. Click pack, all sprite with the same tag will be in one single textrue or atlas.
For conclusion, you can use sprite packer to reduce draw call. Like unity manual said “For optimal performance, it is best to pack graphics from several sprite textures tightly together within a single texture known as an atlas.”