Optimizing Unity UI(二):Fundamentals of Unity UI

Version check: 2017.3 -Difficulty: Advanced

It is important to understand the different parts that make up the Unity UI system. Several basic classes and components make up the system. This chapter first defines some terms used in this series of articles, and then discusses the low-level behavior of several key UnityUI systems.

Terminology

Canvas is a native code Unity component used by Unity's rendering system to provide layered geometric figures that will be drawn on or on the world space of the game.

The canvas is responsible for combining their constituent geometry into batches, generating appropriate rendering commands, and sending these commands to Unity's graphics system. All of this is done in native C++ code, called rebatch or batch generation. When the canvas is marked as containing geometry that needs to be re-batched, the canvas is considered dirty.

Geometry is the canvas that provides the canvas rendering component.

Sub-canvas is just a canvas component nested in another canvas component. The child canvas isolates the child from the parent; the dirty child will not force the parent to rebuild the geometry, and vice versa. In some edge cases, this is incorrect, such as when changes to the parent canvas cause the child canvas to be resized.

Graphic (Graphic) is the base class provided by UnityUIC# library. It is the base class of all UnityUIC# classes and provides drawable geometry for the canvas system. Most of the built-in UnityUI graphics are implemented through MaskableGraphics subclasses, so that they can be shielded through a non-maskable interface. The main subclasses of Drawable are images and text, and they provide components with the same name.

The Layout component controls the size and position of the RectTransform, and is usually used to create complex Layouts that require relative size or relative content positioning . The Layout component only depends on RectTransform and only affects the properties of its associated RectTransform. They do not depend on the graphics class and can be used independently of the graphics components of UnityUI.

Both graphics and layout components rely on the CanvasUpdateRegistry class, which is not exposed in the UnityEditor interface. This class tracks the collection of layout components and graphics components that must be updated, and triggers the update as needed when the relevant canvas calls the WillRenderCanvases event.

The update of Layout and graphics components is called rebuilding. The reconstruction process will be discussed in further detail later in this document.

Rendering details

When combining user interfaces in UnityUI, remember that all geometric figures drawn by the canvas will be drawn in a transparent queue . That is, the geometry generated by UnityUI will always be mixed with alpha. From a performance point of view, the important thing to remember is that every pixel raster extracted from the polygon will be sampled, even if it It is completely covered by other opaque polygons. On mobile devices, this high-energy level of overdraft can quickly exceed the GPU fill rate capacity.

The Batch building process (Canvases)

The batch build process is a process where the canvas combines grids representing its UI elements and generates appropriate rendering commands to send to Unity's graphics pipeline. The results of this process will be cached and reused until the canvas is marked as dirty, which happens whenever changes are made to its constituent grid.

The grid used by the canvas is extracted from the canvas renderer component attached to the canvas, but is not included in any sub-canvas.

Calculating batches requires classifying the grids by depth and checking whether they have overlaps, shared materials, etc. This operation is multi-threaded, so its performance usually varies greatly between different CPU architectures, especially in mobile SoCs (usually only a few CPU cores) and modern desktop CPUs (usually 4 Or more cores).

The rebuild process (Graphics)

The reconstruction process is where the layout and grid of UnityUI's C# graphical components are recalculated. This is performed in the CanvasUpdateRegistry class. Remember, this is a C# class, and its source code can be found in Unity's Bitbucket .

In CanvasUpdateRegistry, the method of interest is PerformUpdate. This method is called whenever the Canvas component calls the WillRenderCanvases event. This event is called once every frame .

PerformUpdate runs three steps:

  • Dirty layout components are requested to rebuild their layout through the ICanvasElement.Rebuild method.
  • Any registered trimming components (such as masks) are requested to trim any trimming components. This is done through ClippingRegistry.Cull.
  • Dirty graphic components need to rebuild their graphic elements.

For layout and graphics reconstruction, the process is divided into multiple parts. Layout reconstruction runs in three parts (PreLayout, Layout and PostLayout), while graphics reconstruction runs in two parts (PreRender and LatePreRender).

Layout rebuilds

To recalculate the proper positions (and possible sizes) of components contained in one or more layout components, the layout must be applied in its proper hierarchical order. In the GameObject hierarchy, a layout closer to the root may change the position and size of any layouts that may be nested in it, so it must be calculated first.

To this end, UnityUI sorts its list of dirty layout components according to their depth in the hierarchy. Items higher in the hierarchy (that is, with fewer parent transitions) are moved to the front of the list.

Then request a sorted list of layout components to rebuild their layout; this is where the position and size of the UI elements controlled by the layout components are actually changed. For details on how the position of each element is affected by the layout, please refer to UI Auto Layout in the "Unity Manual" .

Graphic rebuilds

When rebuilding the graphics component, UnityUI passes control to the reconstruction method of the ICanvasElement interface. The graphics achieve this and run two different reconstruction steps during the PreRender phase of the regeneration process.

  • If the vertex data has been marked as dirty (for example when the component's RectTransform changes size), the mesh will be rebuilt.
  • If the material data is marked as dirty (for example, when the material or texture of the component is changed), then the material of the attached canvas renderer will be updated.

Graphic reconstruction does not pass through the graphic component list in any particular order, nor does it require any sorting operation.

Official translation, to be continued

Guess you like

Origin blog.csdn.net/Momo_Da/article/details/93530301