Fundamentals of Unity UI

来源:互联网 发布:linux mysql 编辑:程序博客网 时间:2024/05/18 03:13

https://unity3d.com/cn/learn/tutorials/temas/best-practices/guide-optimizing-unity-ui?playlist=30089

 

The core tension when optimizing any Unity UI is the balancing of draw calls with batching costs. 


优化UI的核心用批处理成本的平衡

 

· Excessive GPU fragment shader utilization (i.e. fill-rate overutilization)

· Excessive CPU time spent rebuilding a Canvas batch

· Excessive numbers of rebuilds of Canvas batches (over-dirtying)

· Excessive CPU time spent generating vertices (usually from text)

 

四种常见问题:

过度的GPU像素着色(例如,填充率过度使用)

花费大量的CPU时间进行画布的批处理

大量画布的重建(过脏)

用于生成顶点的CPU时间过多(通常来自文本)

 

 

 

Fundamentals of Unity UI

 

Terminology 术语

Canvases are responsible for combining their constituent geometry into batches, generating the appropriate render commands and sending these to Unity’s Graphics system.All of this is done in native C++ code, and is called a rebatch or a batch build.When a Canvas has been marked as containing geometry that requires rebatching, the Canvas is considered dirty.

 

画布会对它的几何组成进行批处理,生成合适的渲染指令并发送给图形系统。这个过程被称为重新进行批处理或者创建批次。当画布被标记为包含需要重新处理的几何图形时,这个画布被认为是“脏画布”。

 

Geometry is provided to Canvases by Canvas Renderer components.

 

通过Canvas Renderer将几何图形提供给画布

 

A Sub-canvas is simply a Canvas component that is nested inside another Canvas component. Sub-canvases isolate their children from their parent; a dirty child will not force a parent to rebuild its geometry.

 

子画布套嵌在父画布中,子画布将其组成与父画布隔离,一个子“脏画布”不会强迫父画布进行重建几何组成。

 

 

A Graphic is a base class provided by the Unity UI C# library.It is the base class for all Unity UI C# classes that provide drawable geometry to the Canvas system. Most built-in Unity UI Graphics are implemented via the MaskableGraphic subclass, which allows them to be masked via the IMaskable interface.Themajor subclasses of Drawable are Image and Text, which provide their eponymous components.

 

Graphic 是由Unity UI library提供的基类。这个基类给画布系统提供了可绘制的几何图形。大多数内建的通过继承MaskableGraphic 实现。

 

Layout components control the size and positioning of RectTransforms, and are generally used to create complex layouts that require relative sizing or relative positioning of their contents. Layout components rely only on RectTransforms and only affect the properties of their associated RectTransforms. They are not dependent on the Graphic class, and can be used independently from Unity UI’s Graphic components.

 

Layout 组件控制RectTransforms的大小和位置,通常用于创建复杂的布局。Layout组件依赖于RectTransforms并且只影响相关联的RectTransforms属性。不依赖于Graphic可以单独使用。

 

Both Graphic and Layout components rely on the CanvasUpdateRegistry class, which is not exposed in the Unity Editor's interface. This class tracks the set of Layout components and Graphic components that must be updated, and triggers updates as needed when their associated Canvas invokes the willRenderCanvases event.

 

Graphic Layout组件都依赖于CanvasUpdateRegistry 这个类跟踪需要更新的GraphicLayout组件,当关联的画布调用willRenderCanvases 画布事件时触发更新。

 

The updates of Layout and Graphic components is called a rebuild. The rebuild process is discussed in further detail later in this document.

 

Graphic Layout组件的更新被称为是重建。

 

Rendering details 渲染细节

When composing user interfaces in Unity UI, keep in mind that all geometry drawn by a Canvas will be drawn in the Transparent queue.That is, geometry produced by Unity UI will always be drawn back-to-front with alpha blending. The important thing to remember from a performance standpoint is that each pixel rasterized from a polygon will be sampled, even if it is wholly covered by other, opaque polygons.On mobile devices, this high level of overdraw can rapidly exceed the fill-rate capacity of the GPU.

 

当组建UI时,记住,画布绘制的几何图形都会在透明列队中绘制。也就是说,几何图形将会从后向前进行alpha混合渲染。从性能的角度讲,需要注意:每一个多边形的像素都会被取样,即使这个像素被其他不透明的多边形所覆盖。

在移动设备上,过量的重绘将会影响GPU

 

The Batch building process (Canvases) 批处理

The batch building process is the process whereby a Canvas combines the meshes representing its UI elements and generates the appropriate rendering commands to send to Unity’s graphics pipeline.The results of this process are cached and reused until the Canvas is marked as dirty, which occurs whenever there is a change to one of its constituent meshes.

 

创建批处理的过程是:画布合并UI元素的mesh,然后生成合适的渲染指定发送给Unity的图形渲染管道。这个过程的结果会被缓存和重用,直到画布被标记为“脏画布”。(画布的组成网格发生变化的时候

 

The meshes used by the Canvas are taken from the set of Canvas Renderer components attached to the Canvas but not contained in any Sub-canvas.

 

画布使用的meshes是通过画布下的Canvas Renderer组件获取的。不包含任何的子画布。

 

Calculating the batches requires sorting the meshes by depth and examining them for overlaps, shared materials and so on.This operation is multi-threaded, and so its performance will generally be very different across different CPU architectures, and especially between mobile SoCs (which generally have few CPU cores) and modern desktop CPUs (which often have 4 or more cores).

 

计算批次:需要根据深度对网格进行排序,检查它们的重叠部分,共享材料等等。

这个操作是多线程的,因此性能在不同的CPU上会有不同的表现

 

 

 

 

The rebuild process (Graphics)  重建

The Rebuild process is where the layout and meshes of Unity UI’s C# Graphic components are recalculated.

This is performed in the CanvasUpdateRegistry class.

 

重建过程是重新计算Graphic组件的布局和网格,在CanvasUpdateRegistry 类中执行。

 

Within CanvasUpdateRegistry, the method of interest is PerformUpdate. This method is invoked whenever a Canvas component invokes the WillRenderCanvases event. This event is called once per frame.

 

CanvasUpdateRegistry 类中,需要关系PerformUpdate方法。当Canvas组件调用WillRenderCanvases 事件会触发这个方法。这个事件每帧被执行一次。

 

PerformUpdate runs a three-step process:

Dirty Layout components are requested to rebuild their layouts, via the ICanvasElement.Rebuild method.

Any registered Clipping components (such as Masks) are requested to cull any clipped components. This is done via ClippingRegistry.Cull.

Dirty Graphic components are requested to rebuild their graphical elements.

 

 

“脏”布局组件 重新构建布局,通过ICanvasElement.Rebuild方法。

任何注册剪切的组件(如Mask)进行剔除和删减。这是通过 ClippingRegistry.Cull 完成的

“脏”图形组件需要重新构建图形元素

 

 

For Layout and Graphic rebuilds, the process is split into multiple parts. Layout rebuilds run in three parts (PreLayout, Layout and PostLayout) while Graphic rebuilds run in two (PreRender and LatePreRender).

 

对于布局和图形的重建,过程会被分成几个部分

布局重建:PreLayout, Layout and PostLayout

图形重建:PreRender and LatePreRender

 

Layout rebuilds 布局重建

To recalculate the appropriate positions (and potentially sizes) of components contained within one or more Layout componentsit is necessary to apply the Layouts in their appropriate hierarchical orderLayouts closer to the root in the GameObject hierarchy can potentially alter the positions and sizes of any Layouts that may be nested within them, and so must be calculated first.

 

重新计算一个或者多个布局中的组件合适的位置,将布局放置在适当的层次中。

层次结构中更接近根的布局可能会改变任何可能嵌套在其中的布局的位置和大小,因此必须首先计算。

 

To do this, Unity UI sorts the list of dirty Layout components by their depth in the hierarchy Items higher in the hierarchy (i.e. with fewer parent Transforms) are moved to the front of the list.

 

为了做到这点,Unity将“脏布局”通过深度进行排序。层次结构高的排在前面。

 

The sorted list of Layout components is then requested to rebuild their layouts; this is where the positions and sizes of UI elements controlled by Layout components are actually altered.

 

排序后的布局组件进行重建。具体的变化是由具体的UI元素控制。

 

Graphic rebuilds 图形重建

When Graphic components are rebuilt, Unity UI passes control to the Rebuild method of the ICanvasElement interface. Graphic implements this and runs two different rebuild steps during the PreRender stage of the Rebuild process.

 

Graphic组件进行重建,通过ICanvasElement Rebuild 方法进行重建。Graphic实现了这一点,并在重建过程的预编译阶段运行了两个不同的重建步骤。

 

 

· If the vertex data has been marked as dirty (e.g. when the component’s RectTransform has changed size), then the mesh is rebuilt.

· If the material data has been marked dirty (e.g. when the component’s material or texture has been changed), then the attached Canvas Renderer’s material will be updated.

 

如果顶点数据被标记为脏(例如,当组件的RectTransform改变了大小),那么网格就会被重新构建。

如果材料数据被标记为脏(例如,当组件的材质或材质被更改时),那么附加的画布渲染器的材料将被更新。

 

 

原创粉丝点击