Unity performance optimization
2020-11-06 21:04:15 【Posture meow】
- Sprite Packer
Import into by subdivision Unity The texture of , Take advantage of the Sprite Packer Merge the textures that need to be drawn on the screen at the same time in a single image set , Can reduce the Drawcall The cost of
- Separate Canvas
canvas （Canvas） yes Unity UI The basic components of . It generates a grid to render the... Placed on the canvas UI Elements , When UI When the elements change , It will regenerate the mesh and move to GPU Start drawing call , It shows that UI.
Generating these grids consumes a lot of performance , Need to put UI Elements are collected into batch processing , This reduces drawing calls as much as possible . Because the batch generation process consumes a lot of performance , Usually regenerate only when necessary . The problem lies in , When one or more elements change on the canvas , The whole canvas has to be reanalyzed , To get the best way to draw elements .
The elements on each canvas are isolated from the elements of other canvases , So we can use tools to slice the canvas , So as to solve Unity UI Batch processing problem of .
We can also solve this problem by nesting canvases , This allows designers to create large layers UI, And you don't have to worry about different content appearing on multiple canvases . The contents of the child canvas are isolated from the parent canvas and the sibling canvas , They keep their own Geometry , Execute your own batch .
When using the sub canvas to separate the canvas , Try grouping by canvas update time . for example ： Separating dynamic and static elements .
- hide Canvas
Sometimes you need to hide UI Elements and canvases , How to accomplish the task efficiently ？
Ban Canvas Component will prevent the canvas from moving to GPU Start drawing call , So the canvas is no longer visible . However , At this point, the canvas does not discard its vertex buffer , It retains all meshes and vertices , When re enabled, the refactoring process will not be triggered , It only redraws the canvas content .
Besides , Ban Canvas Components don't trigger Canvas Performance consumption is high in the hierarchy OnDisable/OnEnable Callback . Be careful when disabling subcomponents , Notice if it runs per frame of code that consumes a lot of performance .
- Raycast Target
This component will get the input information related to the specific canvas UI Set of elements , Then perform the intersection test , It will target Graphic Raycaster On each interactive canvas UI Elemental RectTransform, Check where the input event occurs .
Turn off static or non interactive elements Raycast Target It will directly reduce Graphic Raycaster The number of intersection tests per frame .
If the world space canvas Event Camera Leave the field blank , This does not mean that the canvas will not receive events . It will use the game's main camera . To determine which camera is the main camera , The canvas will access Camera.main attribute .
according to Unity The code path used , Every frame has one Graphic Raycaster And the world space canvas , The canvas will access 7 To 10 Time Camera.main. Each visit Camera.main Will be called Object.FindObjectWithTag. This is not appropriate at run time .
Cache camera references , Then create a system to track the main camera . If you use the world space canvas , To specify Event Camera, Do not leave the attribute blank . If modification is needed Event Camera, Write code to update Event Camera attribute .
- Layout System
A layout system is a group of continuous layout groups （Layout Group）, They're laying out elements （Layout Element） above . Layout elements are not just named Layout Element The components of , They also include UI Images 、 Text and Scroll Rect Components , and Scroll Rect It's also a layout grouping .
Avoid using layout grouping . Scale layout using anchors . Active with a number of dynamic elements UI On , Consider writing code to calculate the layout , Run the code only when needed , Not every time it changes .
Animator Every frame changes the elements , Even if the values in the animation don't change .Animator No empty command check .
Add only to dynamic elements that change frequently Animator. For elements that change little , Or elements that change only in response to events , Please write your own code or make up the system , recommend DoTween.
Drawcall Optimize the merge , namely Drawcall Batching. By reducing Drawcall Count And the consumption of graphics card performance to improve performance .
stay Unity in ,Batches Value means Drawcall The number of times
1.1 Dynamic Batching
No need for any operation , Just share the material ( Even if it's different Mesh Models can also ), It's automatically merged . It can move and rotate freely . But there are the following requirements for use ：
- The total number of model files does not exceed 900.（ Reuse the same Mesh Not counting ）
- A single object may not exceed 300 spot ,Shader You can have normals UV. But if Shader Used UV0 UV1 Two sets UV, perhaps Tangent If it's tangent , A single object can only be no more than 180 spot
- When game objects use the same model and material , Only the same scaling ( namely xyz Scale proportionally , Floating mantissa can be slightly different ) Will be merged .
- Scene baking ： The same material will not be baked after baking .lightmap There are hidden material parameters ：offset/scale, So use lightmap Objects that are not merged
- Shader Don't use too much Pass: many Pass Of Shader Will destroy Dynamic Batching
1.2 Static Batching
After running the game, multiple models of a set of game objects will be dynamically merged into 1 individual . This group of game objects all use the same material in one Drawcall To complete . These game objects can't move, scale, rotate after running . however Drawcall It's got to be the maximum combination of , And it's not subject to many restrictions of dynamic merging
Be careful ：
Even if objects use the same model , stay batch After each object will create a model corresponding to geometry, In the new Combined Mesh in . So much batch Will increase the memory consumption . For example, the trees in the scene are not suitable for Static Batch, And it's suitable for dynamic merging .
Implementation method :
- MeshRenderer Check Batching Static： Just tick it
- The code uses UnityEngine.StaticBatchingUtility Realization ( It can be called on any platform ):
a. All static objects to be merged ( Don't tick Batching Static) Put in a unified one root
b. StaticBatchingUtility.Combine(root); And then it's merged ！
. Check Batching Static： Fully automatic merge , stay MeshFilter It shows Combined Mesh(root:scene). Can't move after merging
. StaticBatchingUtility： Merge into one game object . After merging, you can move the parent game object
2.Skinned Mesh Renderer
There is no batching, So if it's not a character, try to use less .
Be careful ：
(1) modify Renderer.material Will create a new material, You should use Renderer.sharedMaterial To keep the materials public .
(2) have access to Mesh.Optimize() Optimization model . The import model does not need to call this interface , Import the model to Unity3d Has been automatically processed .
The model created by the code may need to call this interface to optimize .
3. Merger requirements contrast
|Can only be MeshRenderer It can't be SkinnedMeshRenderer||requirement||requirement|
|Shader It has to be single Pass||requirement||Does not require|
|The scene cannot be baked （ It can't be marked as Light map static）||requirement||Does not require|
|The total number of grid model files is required||requirement||Does not require|
|Grid model single file number requirements||requirement||Does not require|
|Limit zoom||requirement||Does not require|
|Limit displacement rotation||Does not require||requirement|
|Dynamic merger|| Low memory consumption
You can move and rotate freely
|There are many demands|
|Mark Light map static Merge||The most convenient and fast , Minimum requirements|| It can't move at all
Can only deal with in Scene Objects placed by default in
|Code StaticBatchingUtility Merge|| Convenient and quick , Less demanding
It can be moved as a whole
It can handle scenes or objects loaded dynamically by code
Code merging is the least required , And it can move as a whole . And dynamic merging can move and rotate , And do Animation Animation . Therefore, it is recommended that StaticBatchingUtility + Dynamic merger .
5. Scene making suggestions
When an artist is making a scene , As far as possible ：
- Static objects ： Use static merge
1 use Mesh Renderer, That's not binding bones
2 No Wrap loop UV The texture of , Try to merge it into a large image set
mark Batching Static that will do
- Dynamic objects ： Use dynamic merge , Animation can be done with Unity Animation to do
1 No bones
2 Not involved in scene baking
3 Same object, same scale
4 The number of model points is less than 300 or 180
5 The total number of model files is less than 900
6 Shader single Pass
Meet the conditions Unity Automatic optimization
( Not meeting the requirements of dynamic merging . Scene animation can be done with skeleton animation . But we should try to avoid or use this method less . Because no Drawcall The optimization efficiency is low ）
- C++ 数字、string和char*的转换
- Won the CKA + CKS certificate with the highest gold content in kubernetes in 31 days!
- C + + number, string and char * conversion
- C + + Learning -- capacity() and resize() in C + +
- C + + Learning -- about code performance optimization
C + + programming experience (6): using C + + style type conversion
Latest party and government work report ppt - Park ppt
Online ID number extraction birthday tool
Field pointer? Dangling pointer? This article will help you understand!
GVRP of hcna Routing & Switching
- LeetCode 91. 解码方法
- Seq2seq implements chat robot
- [chat robot] principle of seq2seq model
- Leetcode 91. Decoding method
- HCNA Routing＆Switching之GVRP
- GVRP of hcna Routing & Switching
- HDU7016 Random Walk 2
- [Code+＃1]Yazid 的新生舞会
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- HDU7016 Random Walk 2
- [code + 1] Yazid's freshman ball
- CF1548C The Three Little Pigs
- HDU7033 Typing Contest
- Qt Creator 自动补齐变慢的解决
- HALCON 20.11：如何处理标定助手品质问题
- HALCON 20.11：标定助手使用注意事项
- Solution of QT creator's automatic replenishment slowing down
- Halcon 20.11: how to deal with the quality problem of calibration assistant
- Halcon 20.11: precautions for use of calibration assistant
- "Top ten scientific and technological issues" announced| Young scientists 50 ² forum
- Reverse linked list
- JS data type
- Remember the bug encountered in reading and writing a file
- Singleton mode
- 在这个 N 多编程语言争霸的世界，C++ 究竟还有没有未来？
- In this world of N programming languages, is there a future for C + +?
- js Promise
- js 数组方法 回顾
- ES6 template characters
- js Promise
- JS array method review
- 【Golang】️走进 Go 语言️ 第一课 Hello World
- [golang] go into go language lesson 1 Hello World