The Canvas is the area for all UI elements to 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.(Canvas是所有UI元素的父节点)
When you create a new UI element, such as an Image using the menu
, a Canvas is automatically created too, if there isn’t already a Canvas in the scene. The UI element is created as a child to this Canvas.(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.(Canvas在场景里是一个矩形区域,很容易定位UI元素)
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.(渲染次序按照Hierarchy里的顺序来排,如果两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.(渲染次序有两种方式设置,一种是在Hierarchy里排序,第二种在脚本里调用SetAsFirstSibling、SetAsLastSibling、SetSiblingIndex设置Transfrom组件)
The Canvas has a Render Mode setting which can be used to make it render in screen space or world space.(设置渲染模式,屏幕or世界)
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 as well.(Screen Space-overlay模式把UI元素摆在屏幕上,位于场景最上,自动适配屏幕的缩放)
UI in screen space overlay canvasThis 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 or changes resolution, or the camera frustrum changes, the Canvas will automatically change size to match as well.(与Screen Space-Overlap类似,Canvas被设置在一个指定Camera前面设定的一段位置上。UI元素被指定的Camera渲染,被Camera的设置影响。例:Camera设置为透视投影,UI元素按照透视渲染,透视变形的程度由Camera的视界控制。如果屏幕尺寸和像素变化,或者Camera的视椎体变化,会自适应。)
UI in screen space camera canvasThis render mode makes the Canvas 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, also sometimes referred to as diegetic interfaces.(这种模式使得Canvas像场景中的任何一个Object,Canvas的Rect Transform和UI元素任意放置取决于在3D场景中的位置。)
UI in world space canvas