当前位置:网站首页>Unity performance optimization

Unity performance optimization

2020-11-06 21:04:15 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
  1. 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 .

  1. 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 :

  1. The total number of model files does not exceed 900.( Reuse the same Mesh Not counting )
  2. 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
  3. 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 .
  4. 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
  5. 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 :

  1. MeshRenderer Check Batching Static: Just tick it
  2. 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]所创,转载请带上原文链接,感谢