UGUI optimization series (1) ------ basic concept

This series is to learn the overall solution of siki academy UGUI - optimization (Unity 2019.1.0f2) notes


github address: https://github.com/BlueMonk1107/UGUISolution
atlas block algorithm address: https://github.com/DaVikingCode/UnityRuntimeSpriteSheetsGenerator
texturepacker official website: https://www.codeandweb.com/texturepacker


1. Basic concepts

1. UI is drawn by grid

Both 3D and 2D are drawn by grid, but the complexity is different
View the grid in unity:
image.png
you can see the newly created text grid

2.DrallCall

Unity performance optimization summary one point 2

3. Fill rate

The pixel fill rate refers to the number of pixels rendered by the graphics processing unit per second. The unit is MPixel/S (megapixels per second), or GPixel/S (gigapixels per second), which is used to measure the current graphics card The most common measure of pixel processing performance. The rendering pipeline of the graphics card is an important part of the display core. It is a set of special channels in the display core responsible for matching colors to graphics. The more rendering pipelines, the higher the working frequency of each group of pipelines (generally the core frequency of the graphics card), the higher the fill rate of the drawn graphics card and the higher the performance of the graphics card, so it can be filled from the pixels of the graphics card The performance of the graphics card can be roughly judged by the rate.
Resolution: The pixels of the width and height of the entire screen, the more pixels, the clearer and more delicate the screen image

4.Batch (batch processing)

Unity performance optimization summary one point 4
**

2. Basic components

1. Canvas: the parent object of the UI, which draws the UI to the screen

First batch process its sub-objects, merge the batches, and then send rendering instructions to unity's graphics system (get the geometry of the component through CanvasRender)

# 封装的C++类
public sealed class Canvas : Behaviour 

2.Graphic: Components that can draw geometric figures need to inherit Graphic

Graphic inherits the ICanvasElement interface, which means that this component needs to be used as a child object of the canvas. If it is not placed under the canvas, normal mesh reconstruction cannot be performed (the ICanvasElement interface contains the Rebuild method)

public abstract class Graphic : UIBehaviour, ICanvasElement

1).dirty : Dirty mark

Whether the mesh has been modified, if it is modified, it will be marked and needs to be rebuilt, so that the modified ones can be displayed. The
main thing to pay attention to is Layout/Material/Vertices
image.png

2).MaskableGraphic: a more important subclass of Graphic

MaskableGraphic inherits IMaskable, UI components that inherit this class can be blocked, such as Image/Text

public abstract class MaskableGraphic : Graphic, IClippable, IMaskable, IMaterialModifier

3).LayoutRebuilder: Sort

public class LayoutRebuilder : ICanvasElement
此类中会调用:CanvasUpdateRegistry中的TryRegisterCanvasElementForLayoutRebuild方法
private static void MarkLayoutRootForRebuild (RectTransform controller)
{
    
    
    if (!(controller == null)) {
    
    
        LayoutRebuilder layoutRebuilder = s_Rebuilders.Get ();
        layoutRebuilder.Initialize (controller);
        if (!CanvasUpdateRegistry.TryRegisterCanvasElementForLayoutRebuild (layoutRebuilder)) {
    
    
            s_Rebuilders.Release (layoutRebuilder);
        }
    }
}
CanvasUpdateRegistry中会将此元素加入重建队列中
public static bool TryRegisterCanvasElementForLayoutRebuild (ICanvasElement element)
{
    
    
	return instance.InternalRegisterCanvasElementForLayoutRebuild (element);
}
private bool InternalRegisterCanvasElementForLayoutRebuild (ICanvasElement element)
{
    
    
	if (m_LayoutRebuildQueue.Contains (element)) {
    
    
		return false;
	}
	return m_LayoutRebuildQueue.AddUnique (element);
}

Those that can be sorted will inherit ILayoutElement (Image/Text):

public class Image : MaskableGraphic, ISerializationCallbackReceiver, 
ILayoutElement, ICanvasRaycastFilter

It is recommended to avoid the use of auto-sort components as the cost of rebuilding is very high

4).CanvasUpdateRegistry

Responsible for the registration of Canvas updates, whether other classes need to be rebuilt is to call the method in this class

protected CanvasUpdateRegistry ()
{
    
    
    Canvas.willRenderCanvases += PerformUpdate;
}
# PerformUpdate中会进行Layout重建/Graphic重建

3. The difference between the implementation of the two masks

1.Mask

剪切逻辑在GetModifiedMaterial方法中 : 主要实现的效果是模板(stencil)缓存

2.RectMask2D

Mask 2D, inheriting the interface IClipper of the clipping part

public class RectMask2D : UIBehaviour, IClipper, ICanvasRaycastFilter
#IClipper主要方法是PerformClipping : 实现剪切逻辑

Because the implementation is different, the performance in the subsequent optimization process is also different

Reference: UGUI source code

Guess you like

Origin blog.csdn.net/dengshunhao/article/details/105404021