It is important to understand the different parts making up the Unity UI system. There are several fundamental classes and components that, together, compose the system. This chapter first defines a number of terms used throughout this series of articles, then discusses the low-level behavior of several of Unity UI's key systems.
理解组成Unity UI系统的不同部分是很重要的。有几个基本类和组件共同组成了系统。本章首先定义了本系列文章中使用的一些术语,然后讨论了UnityUI的几个关键系统的底层行为。
A Canvas is a native-code Unity component that is used by Unity’s rendering system to provide layered geometry that will be drawn in, or on top of, a game’s world-space. 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.
Canvas是Unity的渲染系统使用的原生 Unity组件,用于提供将在游戏世界空间中或其上绘制不同层的几何体。画布负责将其组成的几何体组合成批,生成适当的渲染命令,并将这些命令发送到Unity的图形系统。所有这些都是在本地C++代码中完成的,称为rebatch或a batch build。当画布被标记为包含需要重新匹配的几何体时,画布被认为是脏的。
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, and vice versa. There are certain edge cases where this is not true, such as when changes to a parent Canvas cause a child Canvas to be resized.
Sub-canvas只是嵌套在另一个画布组件中的画布组件。Sub-canvas将子画布与其父画布隔离;脏子画布不会强制父画布重建其几何体,反之亦然。在某些边缘情况下,这是不正确的,例如对父画布的更改导致子画布调整大小。
子canvas和父canvas 的dirty相互独立
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. The major subclasses of Drawable are Image and Text, which provide their eponymous components.
Graphic 是Unity UI C#库提供的基类。它是为画布系统提供可绘制几何图形的所有Unity UI C#类的基类。大多数内置的Unity UI图形是通过MaskableGraphic子类实现的,它允许通过IMaskable接口屏蔽它们。可绘制的主要子类是图像和文本,它们提供了它们的同名功能。
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 components控制RectTransforms的大小和位置,通常用于创建复杂的布局,这些布局需要对其内容进行相对大小调整或相对位置调整。Layout 组件仅依赖于RectTransforms,并且仅影响其关联RectTransforms的属性。它们不依赖于Graphic类,可以独立于unityui的图形组件使用
Layout组件控制着RectTransform的大小和位置,只依靠RectTransform
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 类,它不在Unity编辑器的界面中公开。此类跟踪必须更新的Layout 组件和Graphic 组件集,并在其关联的Canvas 调用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类叫做rebuild
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.
在使用UGUI制作UI时,请牢记Canvas中所有几何体的绘制都在一个透明队列中,这就意味着由UGUI制作的几何体将从始至终伴随着alpha混合,所以从多边形栅格化的每个像素都将被采样,即使它被完全不透明的物体所覆盖。在手机设备上,这种高等级的过度绘制将迅速超过GPU填充频率的承受能力。在移动设备上,这种高级别的过度绘制将迅速超过GPU填充频率的承受能力。
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元素的网格组合起来,并生成适当的渲染命令以发送到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.
画布使用的网格取自附加到画布但不包含在任何子画布中的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).
Batch构建过程是指Canvas通过结合网格绘制它所承载的UI元素,生成适当的渲染命令发送给Unity图形流水线。Batch的结果被缓存复用,直到这个Canvas被标为dirty,当Canvas中某一个构成的网格改变的时候就会标记为dirty。 计算Batch要求按照深度排序网格,测试它们是否有重叠,共享材质等等。这个过程是多线程的,在不同的CPU架构下性能表现非常不同
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. Remember, this is a C# class and its source can be found on Unity’s Bitbucket.
Rebuild过程是指Layout和UGUI的C#的Graphic组件的网格被重新计算,这是在CanvasUpdateRegistry类中执行的。