Unity3d Code and Efficiency optimization Summary

Source: Internet
Author: User
Tags cos mathematical functions pow sin

1.PC platform Words keep the number of vertices displayed in the scene less than 200k~3m, and mobile devices less than 10W, all depending on your target GPU and CPU. 2. If you are using U3d's own shader, select the mobile or unlit directory in the case of poor performance. They are more efficient. 3. Share the material as much as possible. 4. Set the object that does not need to be moved to static so that the engine can perform its batch processing. 5. Do not use light as much as possible. 6. The dynamic light is much less. 7. Try compressing the map format, or replace 32 bits with 16 bits. 8. If you do not need to use fog effect (fog) 9. It is very useful to try occlusionculling in a room with multiple objects in the hallway. If inappropriate, it will increase the burden. 10. Use the Sky box to "Fade Away" objects in the distance. 11.shader uses map blending to replace multi-channel computing. Note the use of float/half/fixed in 12.shader. Do not use complex computational pow,sin,cos,tan,log, etc. in 13.shader. The less fragment 14.shader, the better. 15. Note that there is no extra animation script, the model automatically imported into the U3D will have animated script, a large number of words will seriously affect the consumption of CPU computing. 16. Notice the collision layer of the collision body, the unnecessary collision detection please. 1. Why do I need to optimize for CPU (central processing unit) and GPU (graphics processor)? CPU and GPU have their own compute and transmission bottlenecks, different CPUs or GPUs they are not the same, so your game needs to be developed for the CPU and GPU capabilities of your target users. The 2.CPU and GPU limitations of the GPU generally have a fill rate (fillrate) and memory bandwidth (Bandwidth) limitations, and if your game is much faster in low-quality performance, then you probably need to limit your GPU fill rate. The CPU is generally limited by the number of objects to be rendered, and the CPU sends the rendered object to the GPU command called Drawcalls. Generally speaking, the number of drawcalls need to be controlled, the less the better the performance of the premise. Generally speaking, the computer platform on the Drawcalls thousands of, mobile platform drawcalls within hundreds of. That's almost it. Of course, the above is not absolute, only for a reference. Often rendering (Rendering) is not a problem, both on the GPU and on the CPU. It's probably the problem with your scripting code efficiency, look under profiler. About Profiler Introduction: http://docs.unity3d.com/Documentation/Manual/Profiler.html Note: The Rendertex displayed in the GPUTure. The SetActive () occupancy rate is high because you opened the edit window at the same time, not the u3d bug. 3. There are a lot of calculations on vertex count and vertex calculation for CPU and GPU. The number of vertices rendered in the GPU depends on the complexity of GPU performance and shader, and in general, within millions of vertices on the PC, no more than 100,000 vertices on the mobile platform per frame. Calculations in the CPU are mainly in the skin skeleton calculation, cloth simulation, vertex animation, particle simulation and so on. GPU is a variety of vertex transformations, lighting, map blending and so on. "Personally, I think you should look at your project needs, assuming you are 3d games. Your game needs to be compatible with low-configuration hardware, smooth operation, control hardware fever, but also to achieve a certain effect (lightmap+ fog effect), then the number of vertices must not be high. At this time the 2W vertex of the same screen I think is a more appropriate number, drawcall preferably less than 70. Another, control the heat please control the maximum frame rate, smooth words, the frame rate actually does not need too high. "4. Optimized for CPUs--reduce the number of draw calls in order to render objects to the display, the CPU needs to do some work, such as distinguishing which things need to be rendered, whether the objects are affected by the light, which shader to use and which are used for shader, sending drawing commands, and telling the display driver, Then send a command to tell the video card to delete such. Suppose you have a model on the Qian San corner that uses thousands of triangular models instead of the same GPU, but on the CPU it is very different and consumes much more. To make the CPU less work, you need to reduce the number of visible objects: A. Merge similar models, manually merge in the Model Editor or use Unity's draw call batch to achieve the same effect (draw call batching). Specific methods and considerations see the following links: Draw call batching:http://docs.unity3d.com/documentation/manual/ DRAWCALLBATCHING.HTMLB. Use fewer materials (material) in your project, and combine several separate maps into a larger atlas. If you need a script to control individual material properties, be aware that changing the renderer.material will result in a copy of the material. Therefore, you should use Renderer.sharedmaterial to guarantee the shared state of the material. There is a merge model material good plug-in called Mesh Baker, you can consider the test. C. Use as few rendering steps as possible, such as Reflections,shadows,per-pixel light. D.draw Call BATCHing, the merging of objects will make each object (the merged object) have at least hundreds of triangular faces. Assuming that the merged two objects (manually merged) but do not share the material, there is no performance improvement. A multi-material object is equivalent to two objects without a decal. So, in order to improve the performance of the CPU, you should make sure that these objects use the same stickers. In addition, with the light will cancel the (break) engine draw call BATCH, as for why, view the following: Forward Rendering Path details:http://docs.unity3d.com/documentation /components/rendertech-forwardrendering.htmle. Use related culling quantities to directly reduce the number of draw call, as mentioned below. 5. Optimize the geometric model of the basic two optimization criteria: a. Do not have unnecessary triangular faces. The fewer seams and hard edges in the B.uv map, the better. It is important to note that the graphics hardware needs to handle the number of vertices and is not the same as the hardware reports. Not the hardware that can render several points is a few points. The model processing application pass shows the number of geometric vertices. For example, a model consisting of a number of different vertices. In the video card, some of the collection vertices will be separated (split) into two or more logical vertices for rendering. If there are normals, UV coordinates, vertex colors, this vertex must be detached. So the actual amount of processing in the game is obviously much more. 6. It is certainly the quickest thing to do with light if it is not exhausted. Mobile optimization can use a light map (lightmapping) to bake a static map, in lieu of each light calculation, in the u3d only a very short time can be generated. This method can greatly improve the efficiency, and has better performance (smooth transition processing, as well as additional shadows, etc.). Try not to use true light in your scene on a mobile device or on a low-end computer, using a light map. This method greatly saves CPU and GPU compute, and the CPU gets less DRAWCALL,GPU and requires less vertex processing and pixel rasterization. Lightmapping:http://docs.unity3d.com/documentation/manual/lightmapping.html7. GPU optimizations-image compression and multiple texture formats compressed Textures (image compression): http://docs.unity3d.com/Documentation/Components/class-Texture2D.html image compression will reduce your image size (faster loading of smaller memory spans (footprint)), and greatly improve rendering performance. The compression map is less than the uncompressed32-bit RGBA maps consume much less memory bandwidth. Before the U3d meeting also heard of an optimization, map as far as possible in a size format (512 * 512, 1024 * 1024), so that in memory can be better sorted, and there is no gap between the memory. Whether this is true or false has not been tested. MIPMAPS (multiple texture format): http://docs.unity3d.com/Documentation/Components/ Class-texture2d.html the same as the thumbnail on the Web page, in 3D games we create multiple texture maps for the game's stickers, showing smaller objects in the distance with small stickers, showing larger objects with fine stickers. This makes it much more efficient to reduce the data that is transmitted to the GPU. 8.LOD, Per-layer cull distances, occlusion cullinglod (level of Detail) is a very popular 3D game technology, its function is equivalent to the multi-texture map. The use of high or low-level models to reduce the transfer of data to the GPU and to reduce the vertex calculations required by the GPU is judged by the scale of the model size shown in the screen. Camera layering distance culling (per-layer cull distances): Identifies the level of a small object and then determines whether it needs to be displayed based on its distance from the main camera. Occlusion culling (occlusion culling) is the fact that when an object is completely blocked by another object in front of the camera, blocking is not sent to GPU rendering, which directly lowers draw call. However, sometimes it is computationally expensive to calculate whether or not it is blocked in the CPU, but it is not worth the candle. The following are the relevant uses and descriptions of these optimization techniques: level of Detail:http://docs.unity3d.com/documentation/manual/levelofdetail.htmlper-layer Cull Distances:http://docs.unity3d.com/documentation/scriptreference/camera-layerculldistances.htmlocclusion Culling : Http://docs.unity3d.com/Documentation/Manual/OcclusionCulling.html9. About Realtime Shadows (real-time shading) real-time shadow technology is great, but consumes a lot of computation. For both the GPU and the CPU are expensive burdens, the details are referenced below: http:Docs.unity3d.com/documentation/manual/shadows.html10. GPU optimizations: Efficient shadera. It is important to note that some (built-in) shader are mobile versions of , which greatly improves the performance of vertex processing. There are, of course, some limitations. B. Write your own shader note that complex operator calculations, such as Pow,exp,log,cos,sin,tan, are time-consuming calculations, at most once at each pixel point. It is not recommended to write your own normalize,dot,inversesqart operator, the built-in must be better than yours. C. The need for vigilance is Alpha test, which is time consuming. D. Floating-point type operation: The lower the precision, the faster the floating-point calculation. The--float:32-bit floating-point format in CG/HLSL is suitable for vertex transformation operations, but it is relatively slow. The half:16-bit floating-point format, suitable for mapping and UV coordinate calculations, is twice times the HIGHP type calculation. Fixed:10 bit floating-point format, suitable for color, illumination, and others. Is four times times the HIGHP format calculation. Tips for writing shader optimizations: HTTP://DOCS.UNITY3D.COM/DOCUMENTATION/COMPONENTS/SL-SHADERPERFORMANCE.HTML11. Additional related optimizations: A. To draw call Batching optimization http://docs.unity3d.com/Documentation/Manual/DrawCallBatching.htmlb. To rendering Statistics Window's description and tips: HTTP://DOCS.UNITY3D.COM/DOCUMENTATION/MANUAL/RENDERINGSTATISTICS.HTMLC. Optimization of the role model is recommended to render with a single skin, with minimal material, Less use of bone nodes, mobile device character polygons remain within the 300~1500 (of course, depends on the specific needs), PC platform 1500~4000 (of course, depends on the specific needs). Unity3d Code and Efficiency optimization Summary 1, the use of arrays or ArrayList objects should be aware of length=myarray.length;for (Inti=0;i avoid for (INTI=0;I2, if not necessary to handle each frame, You can process voidupdate () {if (time.framecount%6==0)  { dosomething () at every few frames,  }}3, timed repeat calls can be implemented using the Invokerepeating function, for example, the DoSomething function is executed every 1 seconds after the start of 0.5 seconds: Voidstart () invokerepeating ("DoSomething",  0.5f, 1.0f); 4, use less temporary variables, especially in real-time calls such as Update Ongui. Voidupdate () vector3 pos;pos=transform.position; can be changed to: Privatevector3 pos;voidupdate () pos= Transform.position;5, active garbage Collection voidupdate () if (time.framecount%50==0) System.GC.Collection (), 6, optimize the mathematical operation, try to avoid the use of float, Use int, especially in mobile games, to minimize the use of complex mathematical functions, such as Sin,cos functions. Divide/Multiply, for example: Use x*0.5f instead of x/2.0f. 7, compression  mesh import  3D  model, without affecting the display effect, it is best to open  mesh compression.   Off, Low, Medium, High  These options can be selected as appropriate. It is best to use one material for a single mesh. 8,   run time to minimize  Tris  and  draw calls preview, you can open  stats to see the cost of graphics rendering. Pay special attention to the two parameters  Tris  and  Draw Calls .    generally, to do:  tris  remain at  7.5k  following   ,Draw Calls  remain in  35   below. 9, avoid a lot of use  Unity  own  Sphere  and other built-in  MeshUnity  built-in  mesh, the number of polygons is larger, if the object is not required to be particularly smooth, You can import other simple 3D models instead. 10, if possible, will GAmeobject unnecessary script disable off. If you have a big scene in your game and the enemy's location is thousands of meters unexpectedly, this is where you can disable your enemy AI scripts until they are close to the camera. A good way to turn on or off Gameobject is to use setactiverecursively (false), and the spherical or box-type collider is set to trigger. 11. Delete the empty Update method. When you create a new script from the assets directory, the script includes an Update method that deletes it when you are not using it. 12. Refer to the most logical component of a game object. Some people may write somegameobject.transform,gameobject.rigidbody.transform.gameobject.rigidbody.transform, but do some unnecessary work, you can in the first place to lead With it, like this: Privatetransform mytrans;void Start () mytrans=transform;13, Synergy is a good way. You can use a synergistic program instead of a method that does not have to be executed per frame. (There are also invokerepeating methods that are a good alternative to update). 14, try not to update or fixedupdate using the search method (such as Gameobject.find ()), you can get it in the Start method as before. 15, do not use methods such as SendMessage, he is 100 times times slower than the direct call method, you can directly invoke or through the C # Delegate to achieve. 16, when using JavaScript or boo language, you'd better determine the type of the variable, do not use the dynamic type, which will reduce the efficiency, you can use #pragmastrict at the beginning of the script to check, so that when you compile your game there will be no inexplicable error.

Unity3d Code and efficiency optimization summary

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.