Unity Performance optimization record (1) The use of performance analysis tools

Recently, I have been researching Unity Performance optimization related content and came into contact with several Unity official tools. Here is a summary of how they are used and how to use them in combination.

I mainly use the following tools:

  • UPR(Unity Performance Report)
  • Profiler
  • Memory Profiler

First, let’s talk about the relationship between the three in general: Profiler is a performance analysis tool that comes with Unity Editor. It does not require additional downloads and can collect performance data of CPU, GPU, memory, Render, physics, network, etc. The UPR uses the data of the Profiler for further professional analysis and statistics. The Memory Profiler conducts a special analysis of the memory data of the Profiler, and can collect snapshots of the memory during operation.

There are two points to note here:

  • It is best to package Unity to the corresponding platform (remember to check Development Build when packaging), run it and transfer the performance data back through the data line or wifi. There will be a lot of interference data in the Editor and it will be useless.
  • The performance data sources of these three are the games themselves in Development Build mode, so there may be conflicts. At least UPR and Profiler cannot be used at the same time, so the official UPR doc also said that Autoconnect Profiler cannot be checked.

Tool Introduction

Here is a brief introduction to what these tools can do and what they are good at doing.

Profiler

First of all, the Profiler is introduced. This tool comes with Unity and is opened in the Editor. His functions are very comprehensive, including CPU time-consuming analysis, GPU time-consuming analysis, memory allocation, etc. I will not be specific. Say it in detail,官方文档Very detailed, I will not extract it.

One thing to note may be the CPU analysis tool, which I think Profiler is better at than the other two tools, or more detailed.

I simply took a screenshot to take a look:

We just need to focus on the things in the green box on the way:

  • The largest green box is the real-time operation diagram of the CPU. The abscissa is each frame at runtime, and the ordinate is the total time consumption of the frame. If the time consumption of the frame is 33ms, it means 30FPS, because my The project is locked at 30, so my time consumption fluctuates up and down in 33ms.
  • There are two buttons in the top box, one is Deep Profile. After turning on this option, there will be some more detailed performance data, but the problem is that you may find that your game is getting stuck, so unless You want to locate a specific performance point, don’t turn it on, it will affect the observation. Another button is Call stack, after opening the call stack of each function in each frame will be recorded, the drop-down button shows the collection of those function call stack, the default check is GC. Alloc, note that this indicator means not How much garbage is collected, but how much memory is applied for by the function
  • The Timeline on the left can choose three different modes, not only Timeline, Hierarchy, Raw Hierarchy, Timeline shows the timeline of each function, but if you want to see the specific time consumption of each function, you can choose Hierarchy, If you find a problem with which function, you can choose Calls on the right.

Note that there is a callTo on the right side of the calls panel, and the list in a callFrom is not the call stack, but the function calls the function, that is, each function in the callFrom calls the function you choose.

For other content, you can go and see the official doc for yourself, there is nothing particularly good to pay attention to.

Memory

This tool is not particularly good to say, the function is very simple, so it is very intuitive, that is, you can follow the memory snapshot, see the proportion of each resource in the snapshot, which resource occupies the memory.

UPR

UPR is actually based on the Profiler. If the UPR Package is not installed, no more data will be collected, but the data will be summarized in a more intuitive way. He will directly help you show the functions with a high CPU proportion in a graph., will be marked red for some indicators that exceed normal, and most importantly, he can only look at the data of recent frames like Profiler.

So if you have UPR, in most cases you don’t need Profiler.

How to use

Profiler和Memory

This is very simple. After packaging in Development Build mode, you can connect to the data cable or wifi connection.

For details, see doc: https://docs.unity3d.com/Manual/profiler-profiling-applications.html

There are no scams for the time being.

UPR

Next comes our highlight, Unity Performance Report. There are many supporting tools for the use of this tool, including UPR App, UPR Desktop, UPR Package, and AssetChecker.

To use UPR, we first need to create a project on the website and fill in our Unity Package Name, Unity version, etc. in the project, of which this Package Name is the most important.

Then we can create tests under the project. There are many options to choose from when creating tests. In the advanced options, you can configure parameters such as whether you lock frames. Note that if “Enable memory allocation call stack” is turned on, the game will change, but you can see the call stack of those expensive methods you are concerned about in the report.

Then we can do packaging testing.

If we want to use UPR, we first need to check Development Build when packaging, and we cannot check Autoconnect Profiler, and then it is the normal packaging process.

After the steps of packaging to the device are completed, we need to use the UPR tool if we want to upload performance data to the UPR website for analysis.

Among the tools mentioned at the beginning, UPR App and UPR Desktop are two choices, and you must choose one.

The UPR APP is installed on the mobile phone. We created a test on the UPR website, which will generate a QR code and sessionid. Scanning the QR code with the UPR App will obtain the package name of the project to which we created the test. Then we run it at this time, and the UPR App will help us start the game. As for the operation after startup, I won’t go into details, I just say I can’t remember it, but I’ll understand it after using it.

The author uses the Mac system and uses UPR Desktop to test the devices installed on Android, because this can use UPR Package (a unitypakcage, imported into the project) plus ADB to obtain more detailed data, such as power and temperature, etc

The overall installation and use of the way can refer to the official website, but there are a few places to step on the pit:

  • To use UPR on Mac, you need to install .Net3.0 first.
  • Mac uses UPR through the command line, you need to run two commands before using it
    • sudo chmod +x UnityPerfProfiler
    • sudo spctl --master-disable
  • If you don’t go through adb, then run ‘./UnityPerfProfiler -p < device_ip > -s < session_id >’ directly, this thing doesn’t need PackageName is right, but if you want to connect through adb, and follow the three forward rules set by the official website, the following error still appears: Then you have to see if your packageName is written wrong, or the Player Settings of the project overwrite the android pacakgename, Unity’s package name can not only configure a default at the top, but also configure the overwritten packagename for each platform

Summary of mixed use

Personally, I think the way to be more fluid is:

  • Go to UPR first, you can visually see where there are performance bottlenecks
  • Locate the CPU which function has a bottleneck, combined with local observation Profiler CPU Timeline or Hierachy.
  • Locate memory problems, such as leaks or too large problems, combined with the local Memroy Profiler to see which part of the memory is larger, what exactly occupies the memory, whether it can be optimized, etc.