Unity3d Performance Optimization---Collect a bunch of organized

Source: Internet
Author: User

Http://www.cnblogs.com/willbin/p/3389837.html

Official optimization document-Optimize image performance
Http://docs.unity3d.com/Documentation/Manual/OptimizingGraphicsPerformance.html

Unity3d Performance Optimization Topic
Performance optimization is a process that is cumbersome and involves all aspects of project development, and its essence is to present a rich content as perfectly as possible during the run time.
Optimization can be achieved by optimizing resources, rendering, particle, physics and other modes;
You can also improve the performance of your game by modifying the size of the model, reducing the texture size, and combining some of the related features of Unity3d.
As the mobile device hardware capabilities improve, the use of optimized resources and program efficiency to show more detail is what every developer should think about, which makes optimization a very important part of project development.

***********
Let's start with the next draw call (the less things you do, the faster you run the game):
Each individual part of the game that is shown is placed in a special package, which we call the "draw calls", and the package is delivered to the 3D section on the screen. This is just as much as you want your friends and relatives to get ready Christmas presents to be packaged and then run through the city on time where he should appear. Your CPU to wrap and deliver their work, while consuming a lot of bandwidth, so it's important to finally allocate these critical resources. At the moment, the real scary thing is to start with a vision of the consumption of instructions, and every single splash of blood on the floor is just as much as a character or a dead body: they all consume the same depiction instructions. In addition, there is no more difference.
So how do you lower draw call?? Then we'll use the culling (culling) technique. If this technique is not applied, the computer is rendered regardless of 3721 of all the things in the scene. See also render, see also sent to render. It's silly, isn't it? Have to tell the computer that you
Visible rendering, invisible even. And then there's the
1. Cone culling (Frustum culling) This unity system comes with its own looks, so you don't have to worry about it.
2. Occlusion culling (occlusion culling)

Unity 3 Pro has built-in a powerful occlusion culling plugin Umbra free
Occlusion culling (occlusion culling)
What is the feature of occlusion culling, which is not rendered when an object is obscured by other objects and not within the viewable range of the camera. Occlusion culling is not automatic in 3D graphics calculations. Because in most cases the object farthest from the camera is rendered first, the object that is close to the camera renders and overwrites the previously rendered object (this is called repeated rendering, invalid rendering "Overdraw"). Occlusion culling differs from cone culling. The cone culling is not an object that is not rendered outside the camera's viewing range but is not covered by objects that are obscured by other objects but still within the viewing range. Note that you still benefit from the cone culling when you use occlusion culling (Frustum culling).
***********
The process of creating a frame with Unity (or basically all graphics engines) can be roughly simplified: The engine first passes a simple visibility test, determines what the camera can see, and then places the vertices of these objects (including local locations, normals, Uvs, etc.), and indexes (how vertices form triangles), Transform (that is, the position of the object, rotation, scaling, and camera position, etc.), the relevant light source, texture, rendering method (determined by the material/shader) and other data prepared, and then notify the graphics api--or simply as a notification gpu--start drawing, the GPU based on these data, after a series of operations, Draw thousands of triangles on the screen, eventually forming an image.
In unity, the process of preparing data and notifying the GPU each time the engine is called is a draw call. This process is done on an object-by-item basis, and for each object, not just GPU rendering, the engine/shader material is also a very time-consuming operation. So the number of draw call per frame is a very important performance indicator, for iOS should be controlled within 20 times, this value can be seen in the editor's statistic window. Unity's built-in draw call batching technology, as the name suggests, is the main goal of batch processing of multiple objects in one draw call. As long as the objects are transformed and the materials are the same, the GPU can be processed in exactly the same way, that is, they can be placed in a draw call. The core of the draw call batching technique is to examine the material of all objects to be drawn after the visibility test, divide the same material into a group (a batch), and then combine them into an object (unified transformation) so that it can be used in a draw Call handles multiple objects (actually a combination of objects).
But there is a flaw in the draw call batching that it needs to combine all the objects in a batch to create an object as large as those objects, while at the same time allocating the corresponding size of memory. This not only consumes more memory, it also consumes CPU time. Especially for moving objects, each frame has to be re-assembled, which requires some trade-offs, otherwise not worth the candle. But for stationary objects, only one combination is needed, and then it can be used all the time, much more efficiently. -See more at:http://ravenw.com/blog/2011/10/14/unity-optimization-of-draw-call/#sthash. 0wfh4knx.dpuf

***********

Output settings related to:
1: When the final output, in the Unity-edit-project Setting-player, the other Settings-script call optimization changed to fast, but no exception. Fast but no exceptions

2:unity-edit-project setting-time, change maximum allowed Timestep to 0.1.

3:

General optimization
1: Coupling (combine) optimization
The graphics card is almost equivalent to the rendering consumption of an object containing 100 patches and 1500 patches. So if you have n things of the same material, then unite them into the same object and use a material. Then the rendering consumption of the graphics card will be reduced by N times.

In unity again, this how to do it, in fact, it is quite simple, often see the island demo project people should be very early notice that the inside of the stone These are linked together, the reason is here, he provides the current achievement script implementation of the junction.
First to the island demo's assets/ Script to find CombineChildren.cs and MeshCombineUtility.cs two scripts copied to their own project file (we want to use the former, but he will call the latter, without the latter unity will error, so put the latter in the project no matter how good)
Then put the things in your project with the same materials in an empty object, and then put the CombineChildren.cs on the empty object, get it done!

2: Model
(1) Use only one mesh renderer, less than materials, up to 3
Use one skinned Mesh Renderer per character as much as possible
This is because when a character has only one skinned mesh Renderer, Unity uses the method of visibility clipping and bounding body updates to optimize the motion of the character, which is only started when the role contains only one skinned mesh Renderer.
(2) No more than 30 models of bones.
(3) Minimize face 300-1500
(4) Model try not to separate, if more than one module, the DC will be called multiple times
(5) The general role should not have IK nodes.
This is because most of the character's actions are pre-set and do not require an IK operation for real-time calculations (except for rogdoll), so do not import IK nodes together when the model is imported.
(6) Do not attach Animation Component attaching a Animation part to a static entity although it has no effect on the result, it adds a certain amount of CPU overhead to invoke this component, so remove the component as much as possible.

3: Try not to be like light (pixels Lights)

4: No soft Shadows (soft shadow), or no Shadows (no Shadows)
The light can be used without it, and the shadow can be replaced with a face.

5: Use less real-time lighting and try Lightmap
Http://blog.sina.com.cn/s/blog_409cc4b00100no8y.html
Dynamic real-time lighting is very resource-intensive compared to static lighting. So in addition to the dynamic characters and objects (such as can be beaten around the flying oil barrels) static terrain and buildings, all use Lightmap.
Powerful unity has built-in a powerful lighting map baking tool beast, which is the product of Autodesk Company

6:transform and Ongui (computational optimization is far less than optimization of drawing efficiency, which may be comparable to a few DCs)
http://fredxxx123.wordpress.com/2013/05/22/unity%E6%80%A7%E8%83%BD%E5%84%AA%E5%8C%96%E4%B9%8B%E5%B0%8F%E6%B8%AC%E8%A9%A6/
Transfrom
Less use of transform, more use of mycachedtransform
The place where the object is used for the frequency is the cache of the object first, and the theory avoids this unnecessary marketing.
※ This can also be extended to other property variables. For example: Transform.position.
Directly used version:
public class Testunit:monobehaviour
{
void Update ()
{
var t = this.transform;
t = this.transform;
t = this.transform;
t = this.transform;
t = this.transform;
}
}
Version used by the cache:
public class Testunit:monobehaviour
{
Transform Mytransform;
void Start ()
{
Mytransform = transform;
}

void Update ()
{
var t = this.mytransform;
t = this.mytransform;
t = this.mytransform;
t = this.mytransform;
t = this.mytransform;
}
}
Ongui
Empty object version:
public class Testunit:monobehaviour
{
}
Plus a blank Ongui version:
public class Testunit:monobehaviour
{
void Ongui ()
{ }
}
is actually occupied by Gui.begin () and Gui.end (), the function itself is innocent ~ ~
It's unnecessary to avoid using Ongui (), even if it's not doing anything!
If you want to use it, try to focus on drawing under the same Ongui () to avoid the start end.


7: Relative optimization of dynamic objects

Optimization is divided into two main directions, one is resource-related optimization and engine-related optimization. Resource-related optimizations are roughly divided into dynamic objects, static objects, texture data, audio data, and package data. For dynamic objects such as NPC, monsters, etc., need to control the number of the opposite slice, about 300 to 2000 sides. 1500 sides can reflect the character details, but if it is more people, you may want to reduce the number of polygons, not less than 300. In addition, on the one hand is the control skinned Mesh renderer quantity, on the other hand is the control material quantity in 1 to 3 kinds. It's best to use less than 30 bones, and if you use more bones, you'll consume more CPU, so try to be less than 30 on the mobile platform. Now let's look at other dynamic objects and use dynamic batching to make a batch. This rain effect is not a system, it is a grid with a lot of raindrops to repeat the copy, and then the chaotic movement of the implementation. Each raindrop is not a particle, this can reduce the consumption of a lot of CPU, each whole grid will have a vertex control, by controlling the number of vertices, the system to achieve rain effect, this is a relatively time-saving method.

8: Relative optimization of static objects

Here we look at static objects, static objects are also to control the number of polygons and vertices, top points less than 500. Static does not move, scale, rotate, mark it as static, and of course their material is the same. Do not add animation build, for static objects, this component is meaningless, can throw him away, lose, because this CPU consumption is very objective.

9: Optimization of audio programs
About audio time playback, such as background music, it is recommended to use MP3 compression format, such as sound effects, require the data to load as soon as possible, the data is relatively small, using WAV and AIF uncompressed audio format. About the optimization of the package, many developers will complain that the package is too large, now introduce the way to reduce the package, first use the compressed format of the texture, in the video card compression format, using compressed mesh and animation data. Mesh compression is the first quantitative processing, of course, this compression is guaranteed in the packet is small, but the memory consumption is not reduced, because we did not delete the vertex, but for the animation data, the animation data compression processing reduced, you can reduce the game row layer.


For code to try not to use System.Xml, we recommend using Mono.xml. Enable stripping to reduce the size of the library and use a split method.


10: Engine-related optimization and physical-related optimization
Engine-related optimizations such as lighting settings, succession settings, particle effects, physical effects, and more. That light setting, the light source of all real-time lighting It is very scary, every time the implementation of light represents the consumption of each use, how to optimize? Some people use lightmapping to make static scenes, his advantage is not need to use more than one light, and give the scene a good lighting effect. Some people use light probes instead of real-time lighting, which has the advantage of not having to consume at all and operating at a very high performance level. Sometimes using light probes instead of lighting, he can blend nicely with the scene, and in a corner, the task will be darkened by the shadows. If you really need some real-time light source in the scene, then it must be a real-time light source with optimized settings to control the number of important. If there is a light source in some areas of the cross-light, this time you can set the light pxel, the control of each source will only accept a dynamic light, the number is about 1-2. For real-time shading of lights off, not all platforms support real-time shading and are very expensive to use. With regard to camera settings, the closer the plane is, the less rendering is possible. We recommend the use of layering, such as the distance of the building, for the reduction of the structure of the building far more, if it is flowers, you can use the plane nearest some. Now take a look at the particle effects, particles are also the game needs to be optimized, it is recommended that the maximum number of particles in the screen should not exceed 200, and the maximum number of particles emitted by each emitter should not exceed 50. The particle size also needs to be as small as possible, and eventually the number of pixels on the screen. The pixels in between may be rendered many times, at least four or five times, when it is discovered that the particle system fills more screens with only pixels, which is very expensive for the game and has an impact on other features of the game. On the other hand, for very small particles, try not to turn on the particle collision function.

Now let's take a look at the physics-related optimizations, physically use Sphere Coillider, Box coillider, and so on, to avoid using meh colllider as much as possible. Render settings, avoid using alpha Test, because it is time-consuming and cost-effective. About sttic batching, batch for static objects, there is no limit to the size of geometry data. When an object is merged, it brings some memory consumption, such as objects that control the mesh, and batch merges into large objects. Dynamic batching currently supports only mesh objects that are less than 900 vertices. How to understand 900, in fact, the equivalent of 900 vertex data size of the object, if you use position, normal and UV three properties, then you can only Batch300 a vertex. Objects that are scaled collectively cannot be batch unless their scaling values are the same. Before there was a customer to do special effects, using batch mechanism to merge the patch, and eventually let all the patches share a texture, this time found that these patches are not batch out, resulting in running the game about three skills on more than 10 sets of strokes. For the non-whole user body, their batch needs to be well utilized.

11: Texture Merge Optimization
Now look at the texture merge, texture merging is for the number of special to batch, merging objects first need to merge tools, but also to modify the mesh using the texture of the Uvs, so that they use the texture. Merging textures mainly refers to batch, which improves rendering performance. However, after merging the materials, it is important to note that the script access renderer is copied. /* block culling, it is recommended to use PVS technology. It is recommended that you use custom shader, such as the highlight effect, that the highlight effect may not need to do some incoming ray detection, but simply magnifying his value can also simulate the highlight effect, thereby reducing some consumption.

Another is to use the profiler, through the data he gave targeted optimization. The above is to introduce the optimization of the content, how to make a good optimization, must do a good job planning, to the late will not be very troublesome, if the plan is not done well may bring great pressure on the program, the results may be very optimistic. * * Finally, we must continue to summarize the experiment to achieve their own satisfactory results.


12:
If you want to reduce drawcal, there are two tips.
(1) Do not use unity to bring your own UI or Igui, with Nui or EZ GUI
(2) Create a good gameobject it is best to delete/set Active to false/out of the screen in time. These methods can remove the drawcall caused by the object.


13:

*********************************
*********************************
*********************************
I've been working on Unity's Resource optimization on iOS devices for a while, combined with the official documents of unity and the actual problems that I've encountered, I've listed some of the important information I've seen, and as far as I can quantify it to make it easier for more friends who need to be optimized.

1. Role
Use one skinned Mesh Renderer per character as much as possible
This is because when a character has only one skinned mesh Renderer, Unity uses the method of visibility clipping and bounding body updates to optimize the motion of the character, which is only started when the role contains only one skinned mesh Renderer.
Number of Role Material
2-3 x
Number of bones
Less than 30
Number of patches
300-1500
The general role should have no IK nodes.
This is because most of the character's actions are pre-set and do not require an IK operation for real-time calculations (except for rogdoll), so do not import IK nodes together when the model is imported.

2. Static Entities
Do not attach Animation Component
Attaching a Animation part to a static entity has no effect on the result, but adds some CPU overhead to invoke the component, so remove the component as much as possible.
Grid Top points
Less than 500
UV value range try not to exceed (0, 1) intervals
Try to ensure that the UV values do not cross out, which is useful for future texture flattening optimizations.

3. Topography
The resolution size of the terrain
The length and width are as small as 257. This is because the terrain is too large, will cause a lot of vertex data, to your memory bandwidth has a certain impact, in the current iOS device, memory bandwidth is very limited, need to try to save. At the same time, if you use Unity's own terrain, you must also use occlusion culling, because unity's brush terrain tool, although convenient, but it is framekiller, after brushing, you will find that drawcall increased a lot.
Number of mixed textures
Do not exceed 4. The blending of the terrain is time consuming and should be avoided as much as possible. The textures that can be merged are merged as much as possible.

4. Textures
Texture format
PNG or TGA is recommended. Don't turn into the PVRTC format supported by iOS hardware, because Unity will automatically turn it on when it's released.
Texture size
The length of the width is less than 1024. At the same time should be as small as possible, good enough to ensure that the texture of the memory bandwidth to minimize the impact.
Support Mipmap
It is recommended to generate Mipmap. While this practice increases the size of some applications, when the game is running, the system uses MIPMAP to render as needed, reducing memory bandwidth.
Check Alpha value
If the alpha channel of the texture is 1, the RGB 24-bit texture is used instead of the RGBA 32-bit texture. (automatic detection is said to be internal to Unity)

5. Light source
Number of light sources "Important"
Recommended 1, generally for directional light. The number of "Important" should be as small as possible. The more the number, the more drawcall.
Pixel Light Number
1-2 of them.

6. Particle effects
Maximum number of particles on the screen
Less than 200 particles are recommended.
Maximum number of particles emitted per particle emitter
No more than 50 recommendations.
Particle size
If possible, the size of the particle should be as small as possible. Because the shader of Unity's particle system, whether alpha test or alpha blending, is a big expense. Also, for very small particles, it is advisable to remove the alpha channel from the particle texture.
Try not to turn on the particle collision function.
Very time consuming.

7. Audio
Music that plays longer in the game (such as background music)
Use the compression format of. ogg or. mp3.
Shorter music (e.g. gunfire)
Uncompressed audio format using. wav and. AIF.

8. Camera
Clipping plane
Set the far plane to the right distance. Far-plane over the assembly to add some unnecessary objects to render, reduce efficiency.
Set different far clipping planes according to different objects
Unity provides the ability to set different view distance based on different layers, so we can achieve layering of objects, large object layer set the visible distance, and small object layer can be set to smaller, in addition, Some of the more expensive entities, such as particle systems, can be set smaller and so on.

9. Collision
Try not to Meshcollider
If you can, try not to meshcollider to save unnecessary expenses. If you can't avoid it, try to replace it with fewer patches of mesh or a proxy with less patches.

10. Other
Drawcall
As much as possible to reduce the number of drawcall.
No more than 100 recommended on IOS devices.

The main methods of reduction are as follows: Frustum culling, occlusion culling, Texture Packing.

Frustum culling is built within unity, what we need to do is to find a suitable far clipping plane, occlusion culling, occlusion culling, Unity embedded Umbra, a very good OC library. But occlusion culling is not universal, sometimes OC is slower than not, it is recommended to determine whether their scene is suitable to use OC to optimize before OC;
Texture Packing, or Texture atlasing, is to flatten the same shader texture to reduce draw call based on Unity's static batching characteristics. Suggested use, but also has the disadvantage, that is must be the scene in the distance similar solid texture to flatten, otherwise, will probably increase each frame to render the required texture size, increases the memory bandwidth burden.
This is why there will be "drawcall down, rendering speed has slowed down" reason.

Non-moving objects try to make Static labels
While Unity is automatically optimized for static objects at run time, it should be possible to hook non-running entities to static labels.

Use prefab as much as possible in the scene
Use prefab as much as possible to instantiate objects to reduce the burden of memory bandwidth. Check the prefabtype of the entity and try to turn it into prefabinstance instead of modelprefabinstance.

Unity3d Performance Optimization---Collect a bunch of organized

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.