Unity Learning Note-UI System (i)

Source: Internet
Author: User

Reprint please specify the source:

Enigmajj

Http://www.cnblogs.com/twjcnblog/p/5850648.html

Terms

Canvas is a native component of unity, and Unity's rendering system uses canvas to provide a layered geometry that is drawn in the game world.

The role of canvas is to combine the geometry he contains into batches, generate appropriate rendering commands, and send these commands to Unity's graphics system. All of this is done by C + + code, known as Rebatch or batch build. The canvas is considered dirty when the canvas is marked as requiring rebatch.

Geometry is provided to canvas by the canvas renderer component:

Sub-canvas is another canvas nested within the canvas assembly. Sub-canvas isolates the controls that he contains from his parent canvas. Dirty Sub-canvas does not force his parent canvas to refactor the geometry in the parent canvas, and vice versa (there are special cases, such as when the parent canvas causes Sub-canvas to change in size, Then Sub-canvas had to refactor the geometry he contained).

Graphic is a base class provided by the Unity UI C # Library. All Unity UI C # classes that provide drawing geometry (such as image, Text) to the canvas system inherit from graphic. The vast majority of the built-in Unity UI graphics are implemented through the graphic subclass Maskablegraphic, which can be masked using the Imaskable interface implemented by Maskablegraphic :

The layout component controls the size and position of the recttransform. The layout component relies only on recttransform and affects only the properties of the recttransform that are related to itself. The layout component is not dependent on the graphic class and can be used independently of the graphic component of the Unity UI.

Both the layout component and the graphic component depend on the Canvasupdateregistry class. This class tracks the layout components and graphic components that need to be updated, and triggers the update when the Willrendercanvases event is called by the canvas associated with those components.

updates to layout components and graphic components are called rebuild.

Rendering details

When using the Unity UI, keep in mind that all the geometry rendered by canvas is rendered in the transparent queue. That is, all geometry is rendered using alpha blend in a backward-forward order. From a performance standpoint, it means that each rasterized pixel is sampled, even if the pixel is completely covered by other opaque geometry. On mobile devices, this high level of overdraw will quickly surpass the GPU's fill-rate capabilities.

The Canvas batch build process

The batch build process refers to the process by which the canvas combines the meshes of the UI elements that he contains and generates the appropriate rendering commands to send to the Unity graphics pipeline. The results of the build process are cached and reused until the canvas is marked as dirty, and the canvas is marked as dirty whenever any of the meshes that make up the canvas change.

The mesh used by canvas is obtained from the canvas renderer component attached to the canvas, but not from the Sub-canvas.

Batch calculation needs to be based on the depth of the sorting grid, the need to detect the mesh shared material and so on. This operation is multi-threaded, so his performance is often very different on different CPU architectures, especially in the performance difference between mobile chips (usually only a small number of CPU cores) and desktop CPUs (usually 4 or more CPU cores).

Graphics Rebuild Process

The graphics rebuild process refers to the grid and layout recalculation of the Unity UI C # graphics component. This procedure is performed in the Canvasupdateregistry class through the function performupdate.

The Performupdate implementation process is divided into three steps:

1. Dirty layout components refactor their layouts through function icanvaselement.rebuild

2. Any registered cropping component (for example, masks) trims the clipped component, which is done through the function clippingregistry.cull

3. Dirty graphic components to reconstruct the graphic elements they contain

Layout reconstruction is divided into three parts: prelayout, Layout, and Postlayout. The reconstruction of graphic is divided into two parts: PreRender and Lateprerender:

Layout reconstruction

In order to recalculate the location and dimensions of the components contained in the layout component, it is necessary to apply the layouts in the appropriate hierarchical order. The layouts near the root node is likely to change the position and size of the layouts nested inside him, so it must be calculated first.

The Unity UI sorts the dirty layout components according to their depth in the hierarchy, and the layout of the high-level (that is, a small number of parent transform) is moved to the front of the queue.

The sorted layout components then rebuild their layouts, and the location and dimensions of the UI elements that the layout component controls are changed.

Graphic reconstruction

When the graphic component is rebuilt, the Unity UI uses the Rebuild function of the Icanvaselement interface (see step 3rd). Graphic implements this interface and performs two different rebuild steps during the PreRender phase of the rebuild process:

1. If the vertex data is marked as dirty (for example, the recttransform of the component changes size), the mesh will be rebuilt

2. If the material data is marked as dirty (for example, the material or texture of the component has changed), the material attached to the canvas renderer will be updated

Graphic rebuilds do not need to traverse the graphic component in any particular order, nor do they require any sort operations.

Unity Learning Note-UI System (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.