1. 程式人生 > >Unity2017.1官方UGUI文檔翻譯——Canvas

Unity2017.1官方UGUI文檔翻譯——Canvas

存在 sce mean match any ldr des ima mage

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.

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.

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 uses the EventSystem object to help the Messaging System.

Canvas是一塊區域,所有的UI元素都應該在裏面。Canvas就是一個GameObject掛載了一個Canvas組件,所有的UI元素都必須是Canvas的子節點。

如果場景中不存在Canvas,創建一個新的UI元素,例如從GameObject > UI > Image創建一個Image,會自動創建一個Canvas。創建的UI元素會成為這個Canvas的子節點。

Canvas區域在視圖場景中顯示為一個矩形。這樣不需要通過Game視圖也可以很容易地擺放UI元素。

Canvas 使用EventSystem對象來幫助消息系統

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.

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.

Canvas中UI元素的繪制順序與其在Hierachy中的順序相同。第一個元素先繪制,然後第二個,依次繪制。如果2個元素重疊了,後面繪制的會顯示在先繪制的上面。

通過簡單地拖拽Hierarchy中的元素,可以改變元素的繪制順序。想要用代碼改變元素的順序,可以使用Transform組件中的SetAsFirstSibling、SetAsLastSibling、SetSiblingIndex方法。

Render Modes

渲染模式

The Canvas has a Render Mode setting which can be used to make it render in screen space or world space.

Canvas可以設置渲染模式,讓他渲染在屏幕空間或者世界空間

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會自動改變它的大小來匹配(意思也就是Canvas的大小永遠和屏幕一樣大)

技術分享

UI in screen space overlay 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.

這個模式和前面的覆蓋模式很像,不過在這種渲染模式下,Canvas放在一個指定相機的固定距離前。UI元素被相機渲染,這意味著相機的設置會影響到UI的表現。如果相機設置為透視的,UI元素會被透視渲染,透視變形的程度可以被相機的視野控制。如果屏幕調整大小或分辨率或者改變相機的視錐體,Canvas會自動改變大小去匹配

技術分享

UI in screen space camera 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”.

在這種渲染模式下,Canvas會像場景中的其他物體一樣。Canvas的尺寸可以在RectTransform中手動設置,根據3D空間中的位置,UI元素會渲染在其他物體的前面或後面。這對屬於世界的一部分的UI很有用。這個也被稱為“劇情界面“

技術分享

UI in world space canvas

Unity2017.1官方UGUI文檔翻譯——Canvas