Unity UGUI-Canvas Scaler文档

转载自:http://www.cnblogs.com/SolarWings/p/7954162.html

             &博客园马三小伙儿

一、Canvas简介

  Canvas画布是承载所有UI元素的区域。Canvas实际上是一个游戏对象上绑定了Canvas组件。所有的UI元素都必须是Canvas的自对象。如果场景中没有画布,那么我们创建任何一个UI元素,都会自动创建画布,并且将新元素置于其下。

二、Canvas画布参数与应用

1.创建画布

  当你创建任何一个UI元素的时候,都会自动创建画布。也可以主动创建一张画布:点击GameObject->UI->Canvas即可在Hierarchy面板创建一张画布。

2.画布参数

  下面介绍一下Canvas画布的参数:

  第一个参数RenderMode的渲染模式有三种:Screen Space-Overlay、Screen Space-Camera以及World Space。

  1.Screen Space-Overlay模式

  Screen Space-Overlay(屏幕控件-覆盖模式)的画布会填满整个屏幕空间,并将画布下面的所有的UI元素置于屏幕的最上层,或者说画布的画面永远“覆盖”其他普通的3D画面,如果屏幕尺寸被改变,画布将自动改变尺寸来匹配屏幕,如下图效果:

                                                                     

                              (在此模式下,虽然在Canvas前放置了3D人物,但是在Game窗口中并不能观察到3D人物)

  Screen Space-Overlay模式的画布有Pixel Perfect和Sort Layer两个参数:

  (1)Pixel Perfect:只有RenderMode为Screen类型时才有的选项。使UI元素像素对应,效果就是边缘清晰不模糊。

  (2)Sort Layer: Sort Layer是UGUI专用的设置,用来指示画布的深度。

  2.Screen Space-Camera模式

  Screen Space-Camera(屏幕空间-摄影机模式)和Screen Space-Overlay模式类似,画布也是填满整个屏幕空间,如果屏幕尺寸改变,画布也会自动改变尺寸来匹配屏幕。所不同的是,在该模式下,画布会被放置到摄影机前方。在这种渲染模式下,画布看起来 绘制在一个与摄影机固定距离的平面上。所有的UI元素都由该摄影机渲染,因此摄影机的设置会影响到UI画面。在此模式下,UI元素是由perspective也就是视角设定的,视角广度由Filed of View设置。

  这种模式可以用来实现在UI上显示3D模型的需求,比如很多MMO游戏中的查看人物装备的界面,可能屏幕的左侧有一个运动的3D人物,左侧是一些UI元素。通过设置Screen Space-Camera模式就可以实现上述的需求,效果如下图所示:

                                                                                       

  它比Screen Space-Overlay模式的画布多了下面几个参数:

  (1)Render Camera:渲染摄像机

  (2)Plane Distance:画布距离摄像机的距离

  (3)Sorting Layer: Sorting Layer是UGUI专用的设置,用来指示画布的深度。可以通过点击该栏的选项,在下拉菜单中点击“Add Sorting Layer”按钮进入标签和层的设置界面,或者点击导航菜单->edit->Project Settings->Tags and Layers进入该页面。

    可以点击“+”添加Layer,或者点击“-”删除Layer。画布所使用的Sorting Layer越排在下面,显示的优先级也就越高。

  (4)Order in Layer:在相同的Sort Layer下的画布显示先后顺序。数字越高,显示的优先级也就越高。

  3.World Space

  World Space即世界控件模式。在此模式下,画布被视为与场景中其他普通游戏对象性质相同的类似于一张面片(Plane)的游戏物体。画布的尺寸可以通过RectTransform设置,所有的UI元素可能位于普通3D物体的前面或者后面显示。当UI为场景的一部分时,   可以使用这个模式。

  它有一个单独的参数Event Camera,用来指定接受事件的摄像机,可以通过画布上的GraphicRaycaster组件发射射线产生事件。

  这种模式可以用来实现跟随人物移动的血条或者名称,如下图所示:

                                                                           

  我们通过下面的表格可以对比一下三种渲染模式的区别:

渲染模式 画布对应屏幕 摄像机 像素对应 适合类型
Screen Space-Overlay 不需要 可选 2D UI
Screen Space-Camera 需要 可选 2D UI
World Space 需要 不可选 3D UI

Canvas Scaler

画布缩放器

The Canvas Scaler component is used for controlling the overall scale and pixel density of UI elements in the Canvas. This scaling affects everything under the Canvas, including font sizes and image borders.

“画布缩放器”组件用于控制画布中UI元素的整体比例和像素密度。此缩放会影响“画布”下的所有内容,包括字体大小和图像边框。

Properties

属性

 

Details

细节

For a Canvas set to ‘Screen Space - Overlay’ or ‘Screen Space - Camera’, the Canvas Scaler UI Scale Mode can be set to Constant Pixel Size, Scale With Screen Size, or Constant Physical Size.

对于设置为“Screen Space - Overlay”或“Screen Space - Camera”的画布,可以将画布缩放器UI缩放模式设置为Constant Pixel Size, Scale With Screen Size或Constant Physical Size(就是上面提到的那些)

Constant Pixel Size

固定的像素大小

Using the Constant Pixel Size mode, positions and sizes of UI elements are specified in pixels on the screen. This is also the default functionality of the Canvas when no Canvas Scaler is attached. However, With the Scale Factor setting in the Canvas Scaler, a constant scaling can be applied to all UI elements in the Canvas.

使用固定像素大小模式,UI元素的位置和大小在屏幕上以像素指定。 当没有连接Canvas Scaler时,这也是Canvas的默认功能。 但是,通过Canvas Scaler中的Scale Factor设置,常量缩放可以应用于Canvas中的所有UI元素。

Scale With Screen Size

随着屏幕尺寸缩放

Using the Scale With Screen Size mode, positions and sizes can be specified according to the pixels of a specified reference resolution. If the current screen resolution is larger than the reference resolution, the Canvas will keep having only the resolution of the reference resolution, but will scale up in order to fit the screen. If the current screen resolution is smaller than the reference resolution, the Canvas will similarly be scaled down to fit.

使用Scale With Screen Size模式,可以根据指定参考分辨率的像素来指定位置和尺寸。 如果当前的屏幕分辨率大于参考分辨率,画布将保持仅具有参考分辨率的分辨率,但将按比例放大以适应屏幕。 如果当前屏幕分辨率小于参考分辨率,Canvas将同样按比例缩小以适应。

If the current screen resolution has a different aspect ratio than the reference resolution, scaling each axis individually to fit the screen would result in non-uniform scaling, which is generally undesirable. Instead of this, the ReferenceResolution component will make the Canvas resolution deviate from the reference resolution in order to respect the aspect ratio of the screen. It is possible to control how this deviation should behave using the Screen Match Mode setting.

如果当前的屏幕分辨率和参考分辨率的宽高比不同,逐个缩放每个轴以适应屏幕将导致非均匀的缩放,这通常是不期望的。 与此相反,ReferenceResolution组件将使Canvas分辨率偏离参考分辨率,以便尊重屏幕的宽高比。 使用Screen Match模式设置可以控制偏离的方法。

Constant Physical Size

固定的物理尺寸

Using the Constant Physical Size mode, positions and sizes of UI elements are specified in physical units, such as millimeters, points, or picas. This mode relies on the device reporting its screen DPI correctly. You can specify a fallback DPI to use for devices that do not report a DPI.

使用Constant Physical Size模式,UI元素的位置和大小以物理单位(例如毫米,点或皮卡)指定。 该模式依靠设备正确报告其屏幕DPI。 您可以指定fallback DPI以用于不报告DPI的设备(不报告则采用fallback DPI作为默认值)。

World Space

世界空间

For a Canvas set to ‘World Space’ the Canvas Scaler can be used to control the pixel density of UI elements in the Canvas.

对于设置为“World Space”的画布,可以使用Canvas Scaler来控制画布中UI元素的像素密度。

余生很长,愿我们都活成自己喜欢的样子
原文地址:https://www.cnblogs.com/bananana/p/8611777.html