Unity performance optimization strategy

Profiler

Profiler is a tool officially provided by unity to detect running efficiency. Press Ctrl+7 in the Unity panel to call up the tool panel.
insert image description here
Unity provides many types, but we only use three commonly used, CPU, Rendering (rendering), and Memory (memory).

Steps for usage

Click on the CPU module, then change the panel property to Hierarchy, then we write a simple script and run the test.

public class ProfilerTest : MonoBehaviour
{
    
    
    void Update()
    {
    
    
        Test();
    }

    void Test()
    {
    
    
        Debug.Log("AAA");
    }
}

insert image description here
When the program is running, click anywhere in the Cpu panel, and we find that the property panel below has displayed the performance consumption of our script, but it does not show the specific Test method. At this time, we need to check it in the Profiler panel. Deep Profile (can be understood as deep detection), and then Reload.
insert image description here
Then we run it again.
insert image description here
Next we found that the specific Test method was displayed, and then the Debug.log in the Test.

Notes on using Deep Profile

When using DeepProfile, performance will be consumed. If it is in a combat situation, or if the consumption is relatively large, using Deep Profile will have the opposite effect, so we have two situations to avoid.
1: Separately extract the functional modules you want to detect and test them separately.
2: If the coupling of functional modules is serious and it is difficult to extract them separately, we can use the methods provided in the Profiler class.

public class ProfilerTest : MonoBehaviour
{
    
    
    void Update()
    {
    
    
        Profiler.BeginSample("Current Profiler");
        Test();
        Profiler.EndSample();
    }

    void Test()
    {
    
    
        Debug.Log("AAA");
    }
}

insert image description here
It can be seen that the Profiler detects a fixed method performance consumption, which is the same overhead as above.

work complaints

When I was working at home, the project team was newly opened. The main project was a newcomer with less than two years of work experience, and he came out of a training class. I really vomited. Then he didn’t know how to test it in detail, so he just took a simple test and found that the script I wrote was relatively expensive, and then asked me to change it, and then I used Deep Profile to test it out that I called him to save the data method caused a 3MB overhead, and then I asked him to change it back. It is really tiring to meet this kind of colleague. Every job is like kicking the ball. Every time I have to throw the data in his face in real time before he is willing to do it. Then! I just quit.
v0.2 version, new profiler introduction

Optimization is divided into two categories:
Rendering optimization (GPU):
GPU optimization is mainly for the parameter DrawCall. What is the specific parameter of this parameter can be Baidu, so I won’t explain too much here.
To give the simplest example, copying a 1G file to another location and copying 1024 1M files to another location is definitely faster than copying 1G.
So DrawCall is the same. Every time DrawCall is called, it can be understood as preparation work and aftermath work. What we need to do is to merge DrawCall that can be merged to reduce the number of preparation work and aftermath work.
1: Level of detail LOD technology
means that objects rendered at different distances are different. For example, a high-precision model is rendered at a short distance, and a low-precision model is rendered at a long distance.
The main component used is the LOD Group.
insert image description here
Three levels add different models respectively.

2: Occlusion culling
Change the game object that needs to be rendered to
insert image description here
In window=》Occlusion, set the parameters as follows
insert image description here
Bake and select camera to achieve optimization, that is, only render objects in the target field of view.
insert image description here
3: Lightmap merging
For details, please refer to this article of mine.
Lighting and Rendering

4: Mesh merging
All objects are built with three points and one side, and will be drawn by MeshRender in the end, but too many will increase the number of DrawCalls, so we can merge Mesh, and then hand it over to Mesh for rendering.

    void MeshCombine()
    {
    
    
        MeshFilter[] filters = GetComponentsInChildren<MeshFilter>();

        CombineInstance[] combiners = new CombineInstance[filters.Length];

        for(int i = 0; i < filters.Length; i++)
        {
    
    
            combiners[i].mesh = filters[i].sharedMesh;
            combiners[i].transform = filters[i].transform.localToWorldMatrix;
        }

        Mesh finalMesh = new Mesh();
        finalMesh.CombineMeshes(combiners);

        GetComponent<MeshFilter>().sharedMesh = finalMesh;
    }

CPU optimization:
application of object pool , etc.

Guess you like

Origin blog.csdn.net/qq_40629631/article/details/111911763