当前位置:网站首页>Unity performance optimization
Unity performance optimization
2020-11-06 21:04:00 【Posture meow】
Texture Optimization
- 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
UGUI Optimize
- Canvas
- 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 .
- Camera
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
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
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.Mesh Renderer
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 !
difference :
. 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
requirement | dynamic | static state |
---|---|---|
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 |
4. summary
Merger method | advantage | shortcoming |
---|---|---|
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 Memory consumption 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 |
Memory consumption |
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
Conditions
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
Method
mark Batching Static that will do - Dynamic objects : Use dynamic merge , Animation can be done with Unity Animation to do
Conditions
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
Method
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 )
版权声明
本文为[Posture meow]所创,转载请带上原文链接,感谢
边栏推荐
- How about small and medium-sized enterprises choose shared office?
- Summary of front-end performance optimization that every front-end engineer should understand:
- DC-1靶機
- ES6 learning notes (2): teach you to play with class inheritance and class objects
- 【自学unity2d传奇游戏开发】如何让角色动起来
- 如何在终端启动Coda 2中隐藏的首选项?
- html+vue.js 實現分頁可相容IE
- What are PLC Analog input and digital input
- Basic usage of GDB debugging
- PHP application docking justswap special development kit【 JustSwap.PHP ]
猜你喜欢
事务的隔离级别与所带来的问题
es创建新的索引库并拷贝旧的索引库 实践亲测有效!
An article will introduce you to CSS3 background knowledge
Using an example to understand the underlying processing mechanism of JS function
How to turn data into assets? Attracting data scientists
大数据处理黑科技:揭秘PB级数仓GaussDB(DWS) 并行计算技术
To teach you to easily understand the basic usage of Vue codemirror: mainly to achieve code editing, verification prompt, code formatting
ES中删除索引的mapping字段时应该考虑的点
行为型模式之备忘录模式
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?
随机推荐
How to hide part of barcode text in barcode generation software
Use modelarts quickly, zero base white can also play AI!
In depth to uncover the bottom layer of garbage collection, this time let you understand her thoroughly
美团内部讲座|周烜:华东师范大学的数据库系统研究
Flink's datasource Trilogy: direct API
Contract trading system development | construction of smart contract trading platform
解决 WPF 绑定集合后数据变动界面却不更新的问题
StickEngine-架构11-消息队列(MessageQueue)
Kubernetes and OAM to build a unified, standardized application management platform knowledge! (Internet disk link attached)
An article takes you to understand CSS gradient knowledge
Read the advantages of Wi Fi 6 over Wi Fi 5 in 3 minutes
Flink's datasource Trilogy 2: built in connector
如何对数据库账号权限进行精细化管理?
To Lianyun analysis: why is IPFs / filecoin mining so difficult?
What is the meaning of sector sealing of filecoin mining machine since the main network of filecoin was put online
An article will introduce you to CSS3 background knowledge
開源一套極簡的前後端分離專案腳手架
小游戏云开发入门
JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
Even liver three all night, jvm77 high frequency interview questions detailed analysis, this?