<划重点的Unity2017> UGUI

来源:互联网 发布:手动安装windows补丁 编辑:程序博客网 时间:2024/04/27 00:06

1.Canvas
The Canvas is the area that all UI elements should be inside. The Canvas is a Game Object with a Canvas component on it, and all UI elements must be children of such a Canvas.
画布是所有UI元素应该在其中的区域。画布是一个具有Canvas组件的游戏对象,所有UI元素都必须是这样一个Canvas的子项。

Creating a new UI element, such as an Image using the menu GameObject > UI > Image, automatically creates a Canvas, if there isn’t already a Canvas in the scene. The UI element is created as a child to this Canvas.
注意,如果场景当中有一个Canvas,那么UI组件会作为一个子对象创建于Canvas下。

The Canvas area is shown as a rectangle in the Scene View. This makes it easy to position UI elements without needing to have the Game View visible at all times.
画布区域在场景视图中显示为一个矩形。这使得UI元素的定位变得容易,而无需随时看游戏视图。

Canvas uses the EventSystem object to help the Messaging System.
Canvas使用EventSystem对象来触发消息系统。

(1)Draw order of elements

UI elements in the Canvas are drawn in the same order they appear in the Hierarchy. The first child is drawn first, the second child next, and so on. If two UI elements overlap, the later one will appear on top of the earlier one.
Canvas中的UI元素按照它们在层次结构中显示的顺序绘制。第一个孩子最开始绘制,第二个孩子下一个,等等。如果两个UI元素重叠,则稍后的UI元素将出现在较早的UI元素之上。

To change which element appear on top of other elements, simply reorder the elements in the Hierarchy by dragging them. The order can also be controlled from scripting by using these methods on the Transform component: SetAsFirstSibling, SetAsLastSibling, and SetSiblingIndex.
要更改哪个元素显示在其他元素的顶部,只需通过拖动层次结构中的元素重新排序。还可以通过在Transform组件上使用这些方法从脚本控制顺序:SetAsFirstSibling,SetAsLastSibling和SetSiblingIndex。

(3)Render Modes

The Canvas has a Render Mode setting which can be used to make it render in screen space or world space.
画布具有渲染模式设置,可用于使其在屏幕空间或世界空间中呈现。

第一种渲染模式:Screen Space - Overlay
This render mode places UI elements on the screen rendered on top of the scene. If the screen is resized or changes resolution, the Canvas will automatically change size to match this.
此渲染模式将UI元素放置在场景顶部渲染的屏幕上。如果屏幕调整大小或更改分辨率,Canvas会自动更改大小以匹配此选项。

第二种渲染模式:Screen Space - Camera
This is similar to Screen Space - Overlay, but in this render mode the Canvas is placed a given distance in front of a specified Camera. The UI elements are rendered by this camera, which means that the Camera settings affect the appearance of the UI. If the Camera is set to Perspective, the UI elements will be rendered with perspective, and the amount of perspective distortion can be controlled by the Camera Field of View. If the screen is resized, changes resolution, or the camera frustum changes, the Canvas will automatically change size to match as well.
这与Screen Space - Overlay类似,但在这种渲染模式下,Canvas在指定的Camera之前放置一个给定的距离。 UI元素由本相机呈现,这意味着相机设置会影响UI的外观。 如果“相机”设置为“透视”,则UI元素将以透视方式进行渲染,透视失真量可由“相机视野”控制。 如果屏幕调整大小,更改分辨率或相机平截头体更改,Canvas将自动更改大小以匹配。

第三种渲染模式:World Space
In this render mode, the Canvas will behave as any other object in the scene. The size of the Canvas can be set manually using its Rect Transform, and UI elements will render in front of or behind other objects in the scene based on 3D placement. This is useful for UIs that are meant to be a part of the world. This is also known as a “diegetic interface”.
在此渲染模式下,画布将与场景中的任何其他对象一样运行。 可以使用其Rect Transform手动设置Canvas的大小,UI元素将基于3D放置呈现在场景中的其他对象之前或之后。 这对于旨在成为世界一部分的UI是有用的。