为了适应不同的分辨率,我们可能会允许适当的UI整体性缩放,外加一些尽可能少的布局微调。 这样就能达到一个比较理想的效果。Unity为我们提供了Canvas Scaler组件用于控制画布中UI元素的统一缩放和像素密度的。缩放值影响Canvas上所有的元素,包括字体的size和image的borders。
Property: | Function: |
---|---|
UI Scale Mode | Canvas中UI元素的缩放模式 |
Constant Pixel Size | 使UI保持自己的尺寸,与屏幕尺寸无关。 |
Scale With Screen Size | 屏幕尺寸越大,UI越大 |
Constant Physical Size | 使UI元素保持相同的物理大小,与屏幕尺寸无关。 |
Constant Pixel Size:
Property: | Function: |
---|---|
Scale Factor | 通过factor值来缩放所有UI元素(画布大小) |
Reference Pixels Per Unit | 如果Sprite设置了‘Pixels Per Unit’,那么该Sprite一个像素将占用UI的一个像素 |
Scale With Screen Size:
Property: | Function: |
---|---|
Reference Resolution | The resolution the UI layout is designed for. If the screen resolution is larger, the UI will be scaled up, and if it’s smaller, the UI will be scaled down. UI布局的设计分辨率,如果实际分辨率大,UI会放大显示,如果实际分辨率小,UI会缩小显示。 |
Screen Match Mode | A mode used to scale the canvas area if the aspect ratio of the current resolution doesn’t fit the reference resolution Canvas缩放模式 |
Match Width or Height | Scale the canvas area with the width as reference, the height as reference, or something in between 根据设计分辨率的宽,高或按权值取宽高中间值来缩放Canvas |
Expand | Expand the canvas area either horizontally or vertically, so the size of the canvas will never be smaller than the reference 横向或纵向扩展画布大小(因此实际画布尺寸总是大于设计分辨率) |
Shrink | Crop the canvas area either horizontally or vertically, so the size of the canvas will never be larger than the reference 横向或总线收缩画布大小(因此实际画布尺寸总是小于设计分辨率) |
Match | Determines if the scaling is using the width or height as reference, or a mix in between. |
Reference Pixels Per Unit | If a sprite has this ‘Pixels Per Unit’ setting, then one pixel in the sprite will cover one unit in the UI. 如果Sprite设置了‘Pixels Per Unit’为x,那么该Sprite的x个像素将占用UI的一个单位.(UI当中的一单元对应多少设计分辨率像素,最好跟图片设置一致,否则Image显示会与画布不匹配) |
Constant Physical Size:
通过调节 Canvas 物理大小来维持缩放不变。它的意思是在任何屏幕上不改变 Canvas 的 DPI,而是调节 Canvas 的物理大小总是与屏幕保持一致。这种说法可能比 Constant Pixel Size 更难以理解,实际上他们本质是一样的,只不过 Constant Pixel Size 通过逻辑像素大小调节来维持缩放,而 Constant Physical Size 通过物理大小调节来维持缩放。使用这种模式必须指定一个像素转换物理大小的因数(填写96方便在windows上进行开发)。运行时通过具体设备报告的dpi计算 Canvas 像素大小和缩放系数。这种模式从设计的意图来看,是为了在开发时使用物理单位而非像素单位,这只会让程序和美术的工作变得复杂,实际使用价值并不高。因为开发人员更关心设计的像素分辨率,他们需要绘制明确的像素大小的图片!如果未来开发人员和玩家都使用了超高DPI的显示器,那时或许会更注重物理尺寸。
dpi :dots per inch , 直接来说就是一英寸多少个像素点。常见取值 120,160,240。我一般称作像素密度,简称密度
Property: | Function: |
---|---|
Physical Unit | The physical unit to specify positions and sizes in. |
Fallback Screen DPI | The DPI to assume if the screen DPI is not known. |
Default Sprite DPI | The pixels per inch to use for sprites that have a ‘Pixels Per Unit’ setting that matches the ‘Reference Pixels Per Unit’ setting. |
Reference Pixels Per Unit | If a sprite has this ‘Pixels Per Unit’ setting, then its DPI will match the ‘Default Sprite DPI’ setting. 如果Sprite设置了‘Pixels Per Unit’,那么该Sprite一个像素将占用UI的一个像素. |
Settings for World Space Canvas (shown when Canvas component is set to World Space):
Property: | Function: |
---|---|
Dynamic Pixels Per Unit | The amount of pixels per unit to use for dynamically created bitmaps in the UI, such as Text. |
Reference Pixels Per Unit | If a sprite has this ‘Pixels Per Unit’ setting, then one pixel in the sprite will cover one unit in the world. If the ‘Reference Pixels Per Unit’ is set to 1, then the ‘Pixels Per Unit’ setting in the sprite will be used as-is. |
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.
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.
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 then 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.
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.
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.
For a Canvas set to ‘World Space’ the Canvas Scaler can be used to control the pixel density of UI elements in the Canvas.