Unity optimization problem

A: CPU performance optimization

The GPU is different from the CPU , and the focus is also different. The bottleneck of GPU mainly exists in the following aspects:

1. Fill rate, which can be understood as the number of pixels rendered per second by graphics processing

2. Pixel complexity, such as dynamic shadows, lighting, complex shaders , etc.

3. Geometry complexity

4. Of course, there is also the memory bandwidth of the GPU

In view of the above points, it is found that the performance of the image GPU is nothing more than two aspects. On the one hand, the number of vertices is too large and the pixel calculation is too complicated. Another aspect is the memory bandwidth of the GPU .

1. Reduce the number of vertices and simplify the computational complexity

2. Compress images to fit memory bandwidth

reduce the number of draws

Then the first aspect of optimization is to reduce the number of vertices and simplify the complexity. The specific measures are summarized as follows:

1. Keep the number of textures as small as possible. This makes it easier for Unity to do batch processing.

2. Use a texture atlas (a large map contains many sub-maps) instead of a series of separate small maps. They can be loaded faster, have few state transitions, and are more batch friendly.

3. If texture atlas and shared materials are used, use Renderer.sharedMaterial  instead of Renderer.material  .

4. Use lightmaps instead of realtime lights.

5. Using LOD , the advantage is that the details of objects that are far away and cannot be seen clearly can be ignored.

6. Occlusion culling _

7. Use the mobile version of the shader . Because of simplicity.

Optimize memory bandwidth

What about the second direction? Compress images to reduce the pressure on memory bandwidth.

OpenGL ES 2.0 uses ETC1 format compression, etc., in the packaging settings.

Use mipmaps .

B: Program optimization

 01. Be sure to delete the default methods that are empty or unnecessary in the script;
  02. Only use the OnGUI method in one script;
  03. Avoid updating and assigning variables and methods in OnGUI, and output variables are recommended in Update;
  04. Variables frequently used in the same script are recommended to be declared as global variables, and variables or methods frequently called between scripts are recommended to be declared as global static variables or methods;
  05. Do not frequently acquire components and declare them as global variables;
  06. Arrays , Array is used first for collection class elements, followed by List;
  07. The script is disabled when not in use, and then enabled when needed;
  08. Ray can be used instead of the OnMouseXXX class method;
  09. It needs to be hidden/displayed or instantiated to switch back and forth Try not to use SetActiveRecursively or active, but use the practice of moving the object far out of the camera range and back to its original position;
  10. Minimize the use of modulo operations and division operations, such as a/5f, which must be written as a*0.2f .
  11. It is recommended to use Coroutines & Yield for variables or methods that are not frequently called or changed;
  12. Try to directly declare script variables instead of using GetComponent to obtain scripts;
iPhone
  13. Use integer numbers as much as possible, because the floating-point computing capability of iPhone is very high. Poor;
  14. Don't use native GUI methods;
  15. Don't instantiate objects, build object pools in advance, and use Translate to "generate" objects;

C: Model optimization

01. Combine shaders with the same texture, and merge Meshes with the same shader;
  02. There is only one texture and shader for the character. If there are more than one, separate the model into multiple parts;
  02. Do not use too much skeletal system 03. When using multiple
  characters, separate the animations separately;
  04. Use the layer distance to control the display distance of the model;
  05. Shadows actually include two aspects of shadow and shadow. It is recommended to bake the shadow effect when using real-time shadows. Do not use lights to dim the light.
  06. Use less pixel lights and Shaders that use pixel lights;
  08. If hard shadows can solve the problem, don’t use soft shadows, and use low-resolution shadows that don’t affect the effect;
  08. Real-time shadows are very performance-intensive, try to minimize the generation Shadow distance;
  09. Use linear fog in large scenes if allowed, which can make distant objects or shadows imperceptible, so you can improve performance by reducing camera and shadow distances;
  10. Use a smoothing group to minimize the model's Number of faces;
  11. If there are no lights or objects moving in the project, then do not use real-time lights;
  12. Real-time reflection/refraction effects such as water surfaces and mirrors are placed in the Water layer separately, and based on their real-time reflection/refraction range 13. The impact of
  collision on efficiency is small, but it is recommended to use Box and Sphere colliders for collisions;
  14. Try to use Substance when building shaders;
  15. Try to use all real-time reflection/refraction (such as water surface, mirror , floor, etc.) are all assembled into one surface;
  16. It is not necessary to use too large resolution for false reflection/refraction, generally 64*64 is enough, and it is not recommended to exceed 256*256;
  17. The shader that needs to be changed, it is recommended to instantiate one, instead of using a common shader;
  18. Place objects that do not require rays or collision events on the IgnoreRaycast layer;
  19. Place the water surface or similar effects on the Water layer
  20. Place objects with transparent channels on the TransparentFX layer;
  21. Develop good labels ( Tags), hierarchy (Hieratchy) and layer (Layer) organized habits, put different objects in different labels or layers, the effective combination of the three will be very convenient to search by name, category and attribute;
  22 、View the aspects or objects that have the greatest impact on efficiency through Stats and Profile, or use the way of disabling some models to see where the problem is;
  23. Use Occlusion Culling to process large scenes, a relatively native LOD-like technology, And be able to "split" a model as a whole.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324813289&siteId=291194637