Tales from the Profiler - Part 1

By: sleipnir

Action and spectacle weren’t the only targets we wanted to hit with Agent Aliens. Since it’s a fast paced platformer, we also needed precision controls and fluid gameplay. A little stutter or dropped frames might not faze your casual gamer, but for a veteran platformer player it’s an annoyance that would make them drop the game. Fortunately, Unity has a very accurate Profiler and that helped us along the way.

How hard is it to get to 60fps, you ask? To an observer it might appear trivial. Obviously, a lot of older games ran at 60fps even on an SNES or GBA. Our machines are now exponentially more powerful so they should handle a 2D game easily.

To the programmer it isn’t that simple. Back in the old days video game programmers worked very close to the hardware level and made very creative solutions or hacks to squeeze every bit of performance out of the consoles. Today it’s relatively easier to write code for games, in the expense of being layers further away from the hardware level. Most often than not, those layers are black boxes: we do not know what exactly goes on and they might interfere with each other in unknown ways. Unity’s Profiler and it’s accuracy comes into play by helping us untangle those layers and ease the load of the game, making it perform faster on lower end devices.

The tiniest of milliseconds count within each frame of the game.

Isolating spikes is easier with the Deep Profiling feature.

Reduced frame times afer optimization.

Constantly high CPU usage usually comes from using wrong methods of doing a feature entirely. It's a case of getting the job done but being slower and less efficient. The Profiler points to the exact lines of code that are slowing things down and offers clues on how to fix them. When a certain programming pattern is found problematic, the solution can also be applied to similar cases throughout the code. At the end of a hard day bug hunting and optimizing, seeing the game speed up by a few frames makes it all worth it.

That's the Profiler and optimization in a nutshell. In the next two parts we'll go into specific problems we dealt with on Graphics and Unity's Physics. We'll end it here again with a small clip of the game.