7. Debugging and Script Optimization 179
7.6 An Introduction to the Profiler (Pro-Only Feature)
7.6 An Introduction to the Profiler (Pro-Only Feature)
7.6.1 What Is the Profiler?
The profiler is an advanced tool for finding out all kinds of important information about your system resources during playback. It can be used to profile CPU, ren- dering, memory, audio, and physics so that you can find ways to optimize toward a better-performing game.
Open the final example racing project in the Unity editor and press the Play button.
As the game runs, go through the menu Window–>Profiler to open the Profiler window (Figure 7.10). After a short period of time, the Profiler begins to fill up with performance data as it is recorded.
7.6.2 What Do the Squiggles Tell Me?
The profiler takes the form of a timeline, showing the performance data recorded from the time it was launched to the present moment. The vertical white line shows where on the timeline the currently displayed data has come from, and you can move this to focus on data from any recorded time by clicking wherever you would like.
As seen in Figure 7.10, to the left of the timeline are the profilers, each one rep- resenting the types of information being recorded during profiling. You can reorga- nize them (by dragging them around), delete them, or add new profilers from the Add
Figure 7.10. The Profiler window in the Unity editor.
Profiler drop-down menu in the top left of the Profiler window. There are a number of profilers available:
y CPU Usage shows how time is spent in the game and which tasks (or at the very least, which type of tasks) are taking up the CPU during playback.
y Rendering will record data related to rendering, such as the number of draw calls, triangles, VRAM usage, etc.
y Memory records total allocated memory, texture memory, and object count (the total number of objects created in memory).
y Audio captures the number of audio sources playing in the scene at that time (playing sources), the number of paused audio sources (paused sources), the number of audio channels being used, and the amount of memory being used by audio.
y Physics records the number of active rigidbodies and the number of contacts between rigidbodies.
The buttons across the top of the Profiler window are as follows:
y Record toggles on or off performance data recording.
y Deep Profile profiles your code down to every function call. When deep profil- ing is on, only the overall classes will be recorded. Deep profiling will impact performance significantly, as it will be recording every function call across ev- ery script. In some cases, where complex scripting is employed, it may not even be possible to use deep profiling at all.
y View SyncTime gives you more information on wait times. If your game runs at a fixed frame rate or vsync is enabled, there may be an amount of time where the engine waits between frames. By default, this number is hidden, but you can turn it on or off using this button.
y Active Profiler can be used to profile both the Unity editor and external players (such as a web players or iOS players). This drop-down lists available players and allows you to choose one for profiling.
7.6.3 Using the Profilers
First, the bad news: the profiler does not automatically make your game work better and run at a higher number of frames per second. The good news is that the profiler will help to find out which parts of your game are using up the most resources so that you can target them for optimization.
As the profiler begins to capture information, it should quickly become apparent which processes are using the most resources and, if you arrive at the profiler looking for reasons as to why your game might be underperforming, it is usually quite straight- forward to find bottlenecks. Be sure that your profiler captures performance data from
7.6. An Introduction to the Profiler (Pro-Only Feature) 195
the parts of your game you are specifically interested in finding out about by playing the game as it records. Once the profiler has been recording data for a while, you can begin to work through the data. Look toward the areas that have the highest time percentages (look to the Totals column) in the profiler’s Hierarchy window. Those are the ones you need to examine for potential optimization—but if your game is performing well on its target machines, only a minimal number of passes should be necessary through the pro- filer and you should be careful not to get too wrapped up in trying to fix problems that simply are not there just because the profiler shows several items with high percentages.
There will always be some items that will end up higher than you would like, and there are usually trade-offs involved. The trick is, as you attempt to fix performance in one area, don’t ruin performance in another.
As per the mantra elsewhere in this book, keeping the draw calls as low as possible will go a long way in iOS game performance. Avoid transparencies where possible and try to keep the triangle and vertex counts to acceptable levels. If you are still seeing performance issues, they should become apparent quickly in one of the profilers. Of particular interest should be the time spent in your own functions. Also, when doing a deep profile, look to see how much time is spent drawing GUI if you decide to use Unity’s built-in GUI system.
7.6.4 Profiling Live on the iOS Device
One of the most amazing features of the profiler is its ability to run live on the iOS de- vice and display the recorded information in the Unity editor.
To enable profiling on your iOS device, start by connecting it to your Mac devel- opment machine via USB cable. Connect the iOS device to the same Wi-Fi network as your Mac development machine. Wi-Fi is used to carry profiling data from the device to the Unity editor. In the Unity editor, open the Build Settings window (via the menu File–>Build Settings) and check the “Autoconnect Profiler” checkbox.
Click Build and Run to build the game. When Xcode appears (assuming that the build was successful), make sure that the target scheme is set to the name of your device (the scheme is in the top left of the window) and click the Play button to begin playback.
Once the app has copied over onto the device, return to the Unity editor on your development Mac and open up the Profiler window (menu Window–>Profiler). The profiler should begin recording data immediately, and you can proceed with profiling in the same manner you would if it were profiling in editor playback. If the Profiler does not connect to the device automatically, you should use the Active Profiler drop-down menu to select your device from the list.