The Profiler window

Проверено с версией:: 5.4

-

Сложность: Средняя

Introduction

A profiling tool gives detailed information about how our game is performing. If our game has problems, such as low framerate or high memory usage, a profiling tool can show us what is causing these problems and help us fix them.

The Profiler window is a powerful profiling tool that is built into Unity. This article is a guide to what the Profiler window is used for and how to use it. Once we have read this article and are familiar with the layout and functionality of the Profiler window, we can learn how to use the Profiler window to diagnose different types of performance problems.

The Profiler window gives us in-depth information about how different parts of our game are running.

Using the Profiler window, we can learn about different aspects of our game’s performance, such as how our game uses memory, how much CPU time is being used for different tasks, and how frequently physics calculations are being performed. Most importantly, we can use this data to help find the cause of performance problems in our game and measure the effectiveness of our attempts to fix them.

When we have a performance problem with our game, it’s essential to know the cause of the performance problem before trying to fix it. This is because different problems require different solutions. If our project is running slowly, there’s no point trying to improve graphics rendering if the real cause of the problem is overly complex physics! Equally, it’s vital to measure the effectiveness of our attempts to fix performance problems. Effective use of profiling data is the difference between optimization and just changing stuff and hoping for the best.

The layout of the Profiler window

Before we use the Profiler window to gather data on a game, let's open the Profiler window and familiarise ourselves with its layout.

Open the Profiler window from the top menu bar by selecting Window > Profiler

Note that the Profiler window only displays information when profiling data has been recorded. When we first open the Profiler window, some sections will be blank until we record profiling data from a running game.

Profilers

On the left of the Profiler window, you’ll see a column of profilers. Each profiler displays information about a specific aspect of our game. There are separate profilers for CPU usage, GPU usage, rendering, memory usage, audio, physics and networking.

When profiling data has been recorded, the top half of the Profiler window displays data from each profiler over time. Performance can vary over time, so it’s useful to see performance from more than one frame. Some problems may be constant, other problems may appear for only a single frame and other problems may get gradually better or worse as time passes.

The bottom half of the Profiler window displays detailed information from the currently selected profiler about the currently selected frame of data.

The type of data shown here depends on the currently selected profiler. For example, if the memory usage profiler is selected, this area will show information such as which assets are using the most memory and the total amount of memory used. If the rendering profiler is selected, this area will show statistics about the number of objects being rendered or the number of rendering operations being performed.

The profilers provide a lot of detailed information, but we don’t need to use all of them every time we profile; in fact, we would usually begin profiling our game by looking at only one or two of the profilers. As an example, if we had a game that was running slowly we might begin our investigation by looking at the CPU usage profiler.

The CPU usage profiler gives us an overview of which parts of our game are taking the most CPU time to execute. We could use that information to determine which other profilers we should look at. If, for example, we determined that physics functions were taking a long time to run, we would use the Physics profiler to gather for more detailed information about the physics performance in our game.

We can hide profilers so that the display isn’t cluttered with information that we are not interested in. A profiler can be hidden by clicking the X icon in the upper right of its display.

A profiler can be added to the Profiler window by clicking Add Profiler in the top left of the Profiler window and selecting the profiler to add.

Profilers can be added or removed at any time without losing their data. Adding or removing profilers simply shows or hides that data in the display, and it does not clear the data.

Controls

The bar at the top of the screen contains the controls for the Profiler window.

We can use these controls to start and stop profiling, to enable and disable profiling features and to move through the data that the profiler has collected.

A typical use of these controls would be to start profiling our game in time to capture a performance problem and then to stop profiling once we’d gathered the data we wanted. We would then use the timeline controls to step through the data until we reached a frame that shows a performance problem. The data for that frame would be displayed in the lower half of the screen.

Recording profiling data from a game using the Profiler window

Now that we understand more about the layout of the Profiler window, let’s learn how to use it to record data from our game and how to read that data to help us understand how our game is performing.

It’s important to understand that there is a small performance cost when using the Profiler window to record data. This is common to all profiling tools; it’s not possible to record and display in-depth profiling data without some overhead. While this is unlikely to make a significant difference to the way that our game runs, there may be some very minor effects on performance when the Profiler window is recording.

It is possible to profile our game in two different ways: we can profile our game while it is running inside the Unity Editor or we can profile our game while it is running outside of the Unity Editor as a development build. A development build of a Unity game differs from a regular build in two ways: a development build can connect to the Profiler window when the game is running and it includes files that enable debugging.

It is almost always best to profile a development build of our game, rather than profiling our game in from within the Unity Editor. There are two reasons for this. Firstly, the data on performance and memory usage from standalone development build is much more accurate than when profiling a game in the Unity Editor. The Profiler window records data from the Unity Editor itself, which skews the results. Secondly, it is best to test our game on our target hardware whenever possible; for example, if our game is an Android game, we should test it on an Android device. Some performance problems may only appear when the game is running on certain hardware or operating systems, so we may fail to notice problems with our game if we only test it in the Unity Editor.

Having said that, there are times when it may be useful to profile our game in the Unity Editor. These are times when we are more concerned with quickly getting a broad sense of performance, rather than needing very accurate results. For example, we might want to enable and disable lots of GameObjects at runtime to find out which one is responsible for a performance problem. Testing these changes in the Unity Editor will be much quicker than making multiple builds. Once we have found roughly where the problem lies, we can then profile a development build to get the accurate information we need to fix the problem.

Profiling inside the Unity Editor

To record profiling data from our game while it is running inside the Unity Editor, we should follow these steps:

Open the project that you wish to profile in Unity.

Open the Profiler window from the top menu bar by selecting Window > Profiler.

In the tools section at the top of the Profiler window, ensure that Record is selected.

Enter Play Mode.

Profiling data will now be gathered and displayed in real time as you interact with the game.

Profiling a development build of our game

To record profiling data from our our game on target hardware, we need to make a development build of our game and then connect the Profiler window to that build. How we do this depends on the target platform.

Windows, OSX, Linux and WebGL

To build and run a development build of our game on Windows, OSX, Linux and WebGL, we should follow these steps:

Open the project that you wish to profile in Unity.

Open the Profiler window from the top menu bar by selecting Window > Profiler.

In the tools section at the top of the Profiler window, ensure Record is selected.

Open the Build Settings window from the top menu (File > Build Settings).

Ensure that the Development Build checkbox is enabled.

Ensure that the Autoconnect Profiler checkbox is enabled.

Click Build and Run.

The game will run on the target device. When the game is running, profiling data will be displayed in the Profiler window within the Unity Editor in real time as you interact with the game.

Profiling a development build on iOS or Android

Connecting the Profiler window to a development build of a game on iOS or Android is a little more complex. This is because we need to build the game to the device and then connect the device to the Unity Editor.

Full step-by-step instructions for connecting the Profiler window to a development build of a game on iOS and Android can be found on this page of the Unity Manual.

Using the Profiler window to diagnose problems

Now that we understand how the Profiler window works, we can use the Profiler window to identify problems with our game and help us to fix those problems.

If your game is running slowly, stuttering or freezing, this article will show you how to use the Profiler window find the cause of these problems.