Unity UI reduces DrawCall study notes

Summary:

        What is DC: In unity, every time the CPU prepares data and notifies the GPU, it is called a DrawCall.

That is to say, how many DCs there are, how many times the CPU needs to send notifications to the GPU. The more times there are, the greater the impact on performance. Therefore, when developing a project, the number of DCs needs to be strictly controlled. This chapter is the learning record of the blogger on the method of reducing the DC of the UI module. It is suitable for novices. The blogger will write out the steps in detail. If you are already a master, the content of this chapter is relatively simple, but I still hope that if a master sees it, If you have any suggestions or suggestions, feel free to put them in the comment area, thank you very much. If it is helpful to everyone's study, click a little like to encourage novice bloggers.

1. How to view DC

1. If you just want to see the number, you can click the Game view State, and you can see the number of DCs intuitively as shown in the figure below.

Batches: The meaning of batching.

 2. If you want to see what each DC renders, click Window->Analysis->Frame Debugger on the upper menu bar, and a Frame Debug window will pop up as shown in the figure below.

 Select each option on the left, from top to bottom you can see what is rendered at this step.

The picture above is a DC screenshot of an empty scene, the scene has only one camera, and now there are 2 DCs. From the figure above, we know that these two DCs are caused by the Camera. We verify that the Camera is turned off, and we find that there are now 0 DCs.

2. Use Atlas

When making a UI interface, pictures are undoubtedly one of the most frequently used objects. In the case of not using the atlas, we can see that a picture will have a DC when it is put on it, but no matter how many pictures are put on the same picture, it will be a DC. In terms of principle, I won’t go into details here. But through this experiment, it can be seen that if multiple pictures can be converted into one picture, the number of DCs can be greatly reduced. At this time, the function of the atlas is very nice.

  

 What is an atlas? Maybe some students don’t know how to use it. Simply speaking, an atlas can combine multiple small images into a large image. You can right-click Create->SpriteAtlas in the Project window to generate an atlas. Pull the desired picture or the folder where the picture is placed into the Objects for Packing of the atlas, and the picture can be automatically packed into the atlas, as shown in the figure below.

 

△ Note:

1. To use the atlas, you need to open SpritePacker in ProjectSettings, as shown below.

 2. The pictures in the gallery should not exceed the maximum size of the gallery. The size of the current atlas can be seen below. As shown in the picture above, the current atlas is only as large as 64*32. The maximum size is Max Texture Size above. The default is 2048, which means that the atlas can reach up to 2048*2048, and the excess part will not was typed into an atlas.

The blogger's current method of verifying over atlas is a bit dumb, but should work. That is, if you feel that the atlas seems to be full and you are afraid of exceeding it, you can increase the Max Texture Size by one level, for example, the original 2048 is changed to 4096. If it is more than half at this time, it is overflowing. At this time, you need to use the current The project resource considers whether to increase the size or create a new atlas, or to reduce the larger pictures to a smaller size, so that the capacity of the atlas will not overflow, otherwise the DC will explode slowly, which is of course not what we expected.

At this time, be careful not to make the atlas too large in order to reduce DC, because the larger the atlas, the larger the memory. For example, a 2048*2048 atlas is 4M, and a 4096*2048 is 8M. We It is necessary to comprehensively consider the needs of the project and balance the relationship between the two.

As shown in the figure below, the three pictures (same atlas) only occupy one DC.

 3. Text

△ Note:

1. Like pictures, the text of the same font only occupies one DC, so it is best to unify one font for one interface.

2. Because the object is rendered layer by layer, if there is overlap between the text and the image, the number of DCs will also be increased.

 As shown in the figure above, if the text is on the upper layer of the picture, even if it overlaps with the picture, it will not increase the DC, but if you change the hierarchical relationship, as shown in the figure below

If the text overlaps two pictures, even if the two pictures belong to an atlas, an extra DC will be added.

Because DC is simply calculated in this way, if the same layer and the same material will be rendered at the same time, this is considered a DC. If objects with different materials are inserted, the batching will be interrupted and DrawCall will be increased.

     

As shown in the left picture above, if the text is not at the overlap of two pictures but just overlays one picture, then the two pictures still count as one DC, and the text alone counts as one DC. In the picture on the right, there is a text in the overlapping area and a text on the top layer, so that each object counts as a DC.

 You can also take a look at the batch approval of Frame Debug to roughly figure out the reason. Because although the text is overlaid on top of the picture, it is not in their overlapping part, so the two pictures still count as 1 DC.

Therefore, on the premise of not changing the artistic effect, it is best to put all the text on one level (I will get used to keeping the text at the top), which can effectively reduce DC. As shown in the picture below, it is the same as the original effect, but the DC is 2 less.

3. Canvas

If you use the Canvas component to adjust the level of UI objects, but a Canvas will increase 1 DC, use with caution.

 4. Others

Like text, when we use large images, Masks, or when using shaders and other things, a DC will be added. At this time, we should pay attention not to interrupt the batching of the atlas as much as possible, and pay attention to the combination in time. Batch adjustments

V. Summary

To sum up, it mainly depends on Frame Debug, and specific analysis of specific problems.

For the time being, record the above optimization methods first, and then there will be other ways to continue to supplement and adjust.

Guess you like

Origin blog.csdn.net/wang980502/article/details/127173427