Unity3D performance optimization
A project from the middle of production to completion, will certainly encounter a variety of problems, and one of the most able to determine whether the project can be on the line, you need to see whether the performance has been adequately optimized, and determine the performance optimization is also the three : CPU, GPU, and memory.
First, CPU optimization
Optimization of DrawCalls
1.DrawCalls : Each object minimizes the number of renders, and multiple objects are best rendered together.
(1). Use Draw Calls Batahing, which is to draw call batches. Unity3D can merge some objects at runtime to render them with a draw call.
(2). Minimize the use of materials by packing textures into an atlas.
(3). Minimize the use of reflective, shadowy, and other effects, as that will cause the object to render multiple times.
2.Static Batching static batch processing
(1). Static batch operations allow the engine to perform batch operations on geometric objects of any size to reduce drawing calls (as long as these objects do not move and have the same material). Therefore, static batching is more efficient than dynamic batching, and you should use it as low as possible because it requires less CPU overhead.
(2). Explicitly indicate which objects are stationary and never move, rotate and zoom in the game. Check “Static” in the Inspector and Unity will automatically optimize the static objects at runtime. , so as far as possible, non-operational entities should be tagged with static tags.
3 dynamic batch processing
(1). Batching dynamic objects requires a certain overhead on each vertex, so dynamic batching only supports meshes with less than 900 vertices.
(2). If your shader uses vertex positions, normals and UVs, you can only batch objects below 300 vertices;
If your shader needs to use vertex positions, normals, UV0, UV1, and tangent vectors, you can only batch objects below 180 vertices.
Please note that the limit on the number of attributes may change in the future.
(3). Do not use scaling. Two objects with zoom scales (1,1,1) and (2,2,2), respectively, will not be batch processed.
(4). Uniformly scaled objects will not be batched with non-uniformly scaled objects.
Two objects using zoom scales (1,1,1) and (1,2,1) will not be batched, but use two scales (1,2,1) and (1,3,1) The objects will be ready for batch processing.
(5). Using an instance of a different material will cause the batch to fail.
(6). Objects with lightmaps contain additional (hidden) material properties, such as: lightmap offsets and scaling factors. So objects with lightmaps won't be batched (unless they point to the same part of the lightmap)
(7). Presets' instances automatically use the same mesh model and material.
Optimization of physical components
1. Reduce FPS
The VSync Count parameter in ProjectSetting-> Quality will affect your FPS, EveryVBlank is equivalent to FPS=60, EverySecondVBlank = 30;
If neither of these conditions matches the FPS of the game, we need to manually adjust the FPS. First turn off the vertical synchronization feature and then manually set the FPS in the Awake method of the code (Application.targetFrameRate = 45;)
Reduce the benefits of FPS:
(1) Save power and reduce the fever of mobile phones;
(2) Can stabilize game FPS and reduce the occurrence of Caton.
2. Set up a suitable FixedTimestep
This parameter is in ProjectSetting->Time, the purpose is to reduce the number of physical calculations to improve game performance
3. Try not to use MeshCollider
If possible, try not to use MeshCollider to save unnecessary overhead. If it is unavoidable, try to reduce the mesh number of the Mesh or replace it with a smaller patch agent.
Processing memory, triggering GC as little as possible
The GC handles the managed heap and the reference types (class instances, strings, arrays) are assigned to the managed heap. Note that:
- String handling. The old string space will be garbage collected by GC.
- Try not to use the foreach statement, use the for statement. Foreach will bring a lot of garbage.
- Do not directly access the tag attribute of the gameobject. It is better to use if(goCompareTag("human")) instead of if(go.tag=="human") and so on.
- Use pools to achieve space reuse.
- It is best not to use LINQ commands. They will allocate temporary space, which is also the goal of GC collection.
Optimize code quality
- It is best not to use GetComponent frequently, especially in loops
- Good at using OnBecameVisible() and OnBecameVisible() to control the execution of the object's Update() function to reduce overhead
- Use a built-in array, such as Vector3.zero instead of new Vector(0,0,0);
- For the optimization of method parameters, good at using the ref keyword
Second, GPU optimization
GPU and CPU are different, so the focus is not the same. The bottleneck of GPU is mainly the following four aspects:
- The fill rate can be simply understood as the number of pixels per second rendered by the graphics processing unit
- Pixel complexity, such as dynamic shadows, lighting, complex shaders, etc.
- Geometry complexity (number of vertices)
- GPU memory bandwidth
Can be optimized from the following two aspects:
(1) Reduce the number of vertices and simplify the computational complexity
Keep the number of materials as small as possible
Use texture atlas to replace a series of individual tiles
If texture maps and shared materials are used, use Renderer.shaderMaterial instead of renderer.material
Use light textures instead of real-time lighting
The advantage of using LOD is that the details of objects that are far away and not visible can be ignored
Using Occlusion Culling
Use the mobile version of the shader because it is simple
(2) Optimize memory bandwidth
OpenGL ES2.0 uses ETC1 format compression, etc.
Use MipMap
Third, the memory optimization
Character Material Number: 2-3
Number of bones: less than 30
Number of patches: 300-1500
The general character should not have an IK node: This is because most of the character's actions are pre-set and do not require IK operations to perform real-time calculations (except for Rogdoll), so do not use the IK nodes when importing the model. Import together
Static entity: Do not attach Animation Component. Attaching Animation component to static entity will not affect the result, but it will increase the CPU overhead to call this component, so try to remove the component.
Number of mesh vertices: less than 500
Character Material Number: 2-3
The range of UV values should not exceed (0, 1) range as far as possible: Try to ensure that the UV value does not cross the border. This is helpful for future texture combination optimization.
terrain
1. The resolution of the terrain, length and width are less than 257. This is because the terrain is too large and will cause a lot of vertex data, which will cause some impact on your memory bandwidth. In current ios devices, the memory bandwidth is very limited and needs to be saved as much as possible. At the same time, if you use Unity's own terrain, you must also use Occlusion Culling, because Unity's brush terrain tool is convenient, but it is framekiller, after brushing, you will find a lot of drawcall increase
2. The number of mixed textures should not exceed 4. Terrain mixing is time consuming and should be avoided as much as possible. Merge textures as much as possible
Texture
1. The texture format suggests png or tga. There is no need to convert to PRTTC format supported by ios hardware, because Unity will help you to switch automatically when it is released.
2. The length and width of the texture size is less than 1024. At the same time, it should be as small as possible and sufficient enough to ensure that the texture has minimal impact on memory bandwidth.
3. Support Mipmap It is recommended to generate Mipmap. Although this method will increase the size of some applications, when the game is running, the system will use the Mipmap to render according to the demand, thereby reducing the memory bandwidth.
4. Check the alpha value. If the texture's alpha channel is 1, use RGB's 24-bit texture instead of RGBA's 32-bit texture. (It is said that Unity will automatically detect it internally)
light source
1. The number of "Important" light sources is 1, which is generally directed light. The number of "Important" should be as small as possible. The more the number, the more drawcalls.
2. Pixel Light number 1-2.
Audio frequency
1. Use music with long playing time (eg background music) in the game. Ogg or. The mp3 compression format.
2. Use shorter music (such as gunshots). Wav and. Aif's uncompressed audio format.
camera
1. Trim the plane and set the far plane to a suitable distance. Far beyond the plane will add unnecessary objects to the rendering, reducing efficiency.
2. Set different far clipping planes according to different objects
Unity can provide different view distances for different layers, so we can achieve layering of objects, large object layers can be set to a larger visual distance, and small object layers can be set smaller, in addition, some overhead. Larger entities (such as particle systems) can be set smaller and so on.
Particle effects
1. The maximum number of particles on the screen, less than 200 particles are recommended.
2. The maximum number of particles emitted by each particle emitter is recommended to be no more than 50.
3. Particle size If possible, the particle size should be as small as possible. Because the shader of Unity's particle system, whether alpha test or alpha blending, is not a small overhead. Also, for very small particles, it is recommended that the particle texture remove the alpha channel.
4. Try not to open the particle collision function. Very time consuming
From: https://blog.csdn.net/edinburgh_yx/article/details/69569798
Comments
Post a Comment