Unity Q&A-Performance Tuning

1. What is the principle of static batching? Will it cause overhead? If so, what are the costs?

  • Static batch processing: When the game is exported, check static batching in the player setting, and batch processing is performed when the package is exported. The exported package will be larger. Check the static option of the scene in the game scene. When the scene is loaded, a static batch merging will be carried out. The exported package is not large, but the memory will become larger when it is loaded.
  • There are 4 objects in the scene. If you check the static option, the engine will judge whether these 4 objects share the same rendering material during static batch processing. If you share the same rendering material, these 4 objects will be regarded as batchable objects, and the engine will copy out 3 based on the size of a single rendering object, which becomes 4 individual Meshes. At this time, 4 Meshes will have an indexbuffer In this case, the memory usage of resources will be 4 times larger. During rendering, this larger mesh is passed to the GPU for rendering operations.
  • If the CPU is running slowly, the GPU will wait for the CPU. At this time, it is mainly limited by the CPU. The main function of the CPU in the game is to set the rendering state and call the DC. If the material and texture of each object are different, the main job of the CPU is to set the rendering state of the object. There will be more calls to the DC. At this time, the rendering state changes. It consumes more performance and the game runs slower. Therefore, for a large number of objects that do not need to change their positions, static batch processing will be used to solve the bottleneck of the rendering state.

2. What are the conditions for dynamic batching? Will it cause overhead? If so, what are the costs?

  • Dynamic Batching is when the object is less than 300 (regardless of whether the object is static or dynamic), when the same shader is used, unity will automatically merge it into a batch and send it to the GPU for processing.
  • Unity can merge some objects at runtime and render them with one draw call. This operation is called "batch processing". Generally speaking, the more objects unity batch processes, the better rendering performance you will get.
  • The dynamic batch operation is done automatically and does not require you to perform additional operations.
  • Batch processing of dynamic objects requires a certain amount of overhead on each vertex, so dynamic batch processing only supports meshes with less than 900 vertices.
  • The instance of the preset body will automatically use the same network model and material.

3. What are the conditions for GPU Instancing?

  • Instanced objects with different materials will cause batch processing to fail

4. What is Drawcall? Talk about how to reduce Drawcall?

  • In unity, each time the CPU prepares data and notifies the GPU, it is called a DrawCall.
  • The specific process: set color -> drawing mode -> vertex coordinates -> draw -> end. Therefore, in the drawing process, if all the drawing can be completed in one DrawCall, the operating efficiency will be greatly improved, and the goal of optimization will be achieved.
  • why: In order for the CPU and GPU to work in parallel, a command buffer is needed. The CPU adds commands to it, and then the GPU reads the commands from it. This enables the CPU to prepare data and notify the GPU to render. Before calling DrawCall each time, the CPU needs to send a lot of content to the GPU, including data, rendering status, commands, etc.
  • The specific operations of the CPU are: 1. Prepare the rendering object, and then load the rendering object from the hard disk to the memory, and then from the memory to the video memory, thereby facilitating high-speed processing by the GPU. 2. Set the rendering state of each object, that is, set the object's material, texture, shader, etc. 3. Output the rendering primitives, and then send the DrawCall command to the GPU, and pass the rendering primitives to the GPU. Therefore, too many DrawCalls will lead to a large number of CPU calculations, which will lead to CPU overload and affect the efficiency of the game.
  • optimization:
  1. Regarding the processing of atlas, materials, and levels, reduce DrawCall. Only objects of the same material can be batch processed, and materials and objects are reused as much as possible in the program. If the two materials only have different textures, you can merge the two textures into one large texture through the texture stitching operation, and you can replace the previous two materials with a single material.
  2. Dynamic batch processing. If dynamic objects share the same material, unity will automatically batch process these objects without additional operations.
  3. For complex static scenes, you can consider designing your own occlusion culling algorithm to reduce the number of visible objects and also reduce DrawCall.
  4. Packaged atlas, from a functional point of view: UI can be divided into public parts, and each specific interface, functionally, and display closely related graphics are packaged together. Pay attention to control the size of the atlas, not too big, a super large atlas DrawCall may consume more than a dozen small atlas.
  5. Special effect cleaning: When the special effect is finished, if you don't set its active to fasle, it will continue to occupy DrawCall and consume the computing power of the device. Therefore, after a special effect is played, it can be consumed or set to an inactive state, and public methods can be used to complete the cleaning after the special effect is played.

5. What is SetPass Call?

  • The number of Material types contained in all GameObjects within the camera's illumination range.
  • If the value of SetPass Call is low, DrawCall is not necessarily low, but if the value of SetPass Call is high, DrawCall must be high.
  • When optimizing DrawCall, the key point is to reduce the value of Bathes.

6. What are the texture formats of the Android/iOS platforms respectively set to? what is the benefit?

  • The final decision that the picture occupies memory is its pixel format and size, regardless of the extension, png8, png32, jpg, pvr as long as the pixel format is argb8888, then the memory occupied is the same.
  • If it is not in pvrtc4 format, then do not expand to a power of 2. The smaller the picture, the smaller the memory occupied.
  • Simply removing the transparent channel will not reduce the memory consumed by the image, and png and jpg images cannot reduce the image volume. The format of rgb888 is not recommended. Choose rgb565 and rgb5551 instead.
  • If you can choose to reduce the size of the picture: 1. jpg-the highest compression ratio, the best quality, but does not support translucency 2. png8-the same picture will be slightly larger than jpg, use imageAlpha for conversion, almost invisible visually Make a difference. Both of these formats greatly reduce the image size, but do not help reduce memory.

7. What are the general ideas for game optimization?

9. What are the main performance-consuming aspects of the Unity game engine?

10. How does Unity draw 3D objects?

11. Unity reduces GC overhead, what aspects should be paid attention to when writing code?

  • The operation of the program will directly affect the changes in the system environment, thereby affecting the triggering of GC. In order to avoid these effects, the basic principle is to minimize garbage and reduce the overhead in the GC process.
  1. Do not call System.gc() explicitly. In many cases it will trigger the main GC, thereby increasing the frequency of the main GC, and also increase the number of intermittent pauses.
  2. Minimize the use of temporary objects. Temporary objects will become garbage after jumping out of function calls. Using less temporary variables is equivalent to reducing the generation of garbage and reducing the chance of main GC.
  3. It is best to explicitly set the object to Null when not in use. Generally speaking, Null objects will be treated as garbage, so explicitly setting unused objects as Null will help the GC collector to determine as garbage, thereby improving the efficiency of GC.
  4. Try to use StringBuffer instead of String to accumulate strings. A string is a fixed-length string. When accumulating string objects, it is not amplified in a string object, but a new string object is recreated, such as str3 = str1 + str2, and multiple garbage objects will be generated during execution, because New string objects must be created during the "+" operation, but these transitional objects have no practical meaning to the system and will only add more garbage. To avoid this situation, you can use stringbuffer to accumulate strings instead, because stringbuffer is variable-length, it is amplified on the original basis, and no intermediate objects are generated.
  5. If you can use the basic types int and long, you don't need Integer and Long objects. Basic type variables occupy much less memory resources than corresponding objects.
  6. Minimize the use of static object variables.

Guess you like

Origin blog.csdn.net/wang_lvril/article/details/109194573