About unity ui resource optimization

1. Reasonable allocation of atlases
Reasonable allocation of atlases can reduce the speed of drawcalls and resource loading; the details are as follows:
● The pictures of the same UI interface should be placed in one atlas as much as possible to reduce drawcalls as much as possible.
 
● Put shared pictures in one or several shared atlases, such as common bullet boxes and buttons; pictures with the same function are placed in one atlas, such as equipment icons and hero avatars; this can reduce the loading speed of the switching interface.
 
● Put pictures in different formats into different atlases, such as transparent (with Alpha) and opaque (without Alpha) pictures, which can reduce the storage space and memory usage of pictures. (UGUI's sprite packer will automatically handle this situation)
 
2. Only prefab files should be saved in the resources directory, and other non-prefab files (such as animations, textures, materials, etc.) should be placed outside the resources directory
because with the iteration of the project, It may cause some resources (animations, textures), etc. to fail. If these files are placed in the resource directory, when packaging, Unity will type all the text in the resource directory into a large AssetBundle package (the files in the non-resouce directory are only in the The reference will be typed into the package when it arrives), resulting in redundancy, increasing unnecessary storage space and memory usage. You can view the codes of all non-prefab resources in the current directory through the following code (in the Mac environment):
find . -type f | egrep -v "(prefab|prefab\.meta|meta)$"
 
For example, in the author In a scan of , the following results were found:

 
3. The UI resources in the level should not be mixed with the UI resources of the peripheral system
in the level. A large number of characters and scene resources need to be loaded, and the memory is relatively tight. Generally, when entering the level, the resources of the peripheral system will be released manually, so that there are some resources in the level. More memory can be used. If the UI in the battle and the UI of the peripheral system use the same images in the atlas, it may cause the release of the image resources of the peripheral system to fail. The UI resources shared by the level and the periphery need special treatment. Generally speaking, it is a better choice to copy a copy for use in the level.
 
4. Appropriately reduce the size of the image.
Sometimes the background of the UI system may use a full-screen image, such as a 1136*640 image on an Iphone; using such a size image is very expensive, and you can discuss it with your art classmates. to reduce the precision of the image, use a lower size image.
 
5. Use etc format pictures on android devices
At present, almost all android devices support etc1 format pictures. The advantage of etc1 is that the first pixel only uses 0.5 bytes, while ordinary rgba32 pictures occupy 4 pixels per pixel. Bytes, that is to say, a 1024*1024 image uses 4M of memory in the rgba32 format and only 0.5M in the etc1 format. But images using etc1 format have two limitations - length and width must be POT (2 to the Nth power) and do not support alpha channel, so when using etc1, you need an extra image to store the alpha channel, and use a special The shader to sample the alpha. For details, please refer to: http://malideveloper.arm.com/resources/sample-code/etcv1-texture-compression-and-alpha-channels/
 
6. Delete unnecessary UI nodes, animation components and resources
With the iteration of the project, some UI nodes and animations may have expired, and the failed nodes and animations must be deleted. In many projects, some students just disable the failed nodes and animations for convenience. Doing so will not put too much load on the cpu at runtime, but it will increase unnecessary loading time and memory usage when loading. For discarded UI image resources, although they are not placed in the Resource directory, they will not be included in the package, but they will still be included in the atlas in Editor mode, which affects optimization decisions. The author wrote a tool to scan unused UI texture resources, code address: https://github.com/neoliang/FindUnUsedUITexture ;
 

In addition, for abandoned scripts, there may be some objects that hold references to it, and loading such objects is also time-consuming. The author also wrote a tool to scan abandoned scripts, code address: https://github .com/neoliang/MissingScriptFinder


***

Be careful to use enable and disable of UI elements, because they will trigger expensive rebuilds (3 and 4 in the figure). One of the alternatives is to enable and disable the canvasrender or Canvas of UI elements.

Use Text's Best Fit option with caution. Although this option can dynamically adjust the font size to fit the UI layout without exceeding the frame, it is very expensive. Unity will generate graphs for all font sizes used by the element. The meta is stored in atlas, which not only increases the extra generation time, but also makes the atlas corresponding to the font larger.


Be careful with Canvas' Pixel Perfect option, which will cause layout rebuilds when ui elements change positions. (For example, when ScrollRect is scrolling, if the pixel Perfect of Canvas is turned on, the consumption of Canvas.SendWillRenderCanvas will be high)


Use the buffer pool to save the Items in the ScrollView. For the elements moved out of or into the View, do not call disable or enable, but put them in the buffer pool or take them out of the buffer pool for reuse.


The touch processing consumption of UGUI may also become a performance hotspot. Because UGUI calls raycast on all visible Graphic components by default. For grahic that does not need to receive touch events, be sure to disable raycast. For Unity5 and above, the Raycast Target that can close the graphic can be turned off. For Unity4.6, the canvasgroup component can be added to the UI elements that do not need to receive touch.


Mask increases the number of DrawCalls > 1


Image overlap will generate DrawCall increase (backpack Item)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325200224&siteId=291194637