CanvasPanelSlot 布局规则

UMG 中的 CanvasPanelSlot

我们先来看一下在 UI 面板中,CanvasPanelSlot 有哪些信息:

image-20210812153142702

源码中的 CanvasPanelSlot

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class UMG_API UCanvasPanelSlot : public UPanelSlot
{
GENERATED_UCLASS_BODY()

public:

/** The anchoring information for the slot */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Layout|Canvas Slot")
FAnchorData LayoutData;

/** When AutoSize is true we use the widget's desired size */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Layout|Canvas Slot", AdvancedDisplay, meta=( DisplayName="Size To Content" ))
bool bAutoSize;

/** The order priority this widget is rendered in. Higher values are rendered last (and so they will appear to be on top). */
UPROPERTY(EditAnywhere, BlueprintReadOnly, Category="Layout|Canvas Slot")
int32 ZOrder;

/*其余信息*/
}

struct FAnchorData
{
public:
GENERATED_USTRUCT_BODY()

public:

/** Offset. */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=AnchorData)
FMargin Offsets;

/** Anchors. */
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=AnchorData)
FAnchors Anchors;

/**
* Alignment is the pivot point of the widget. Starting in the upper left at (0,0),
* ending in the lower right at (1,1). Moving the alignment point allows you to move
* the origin of the widget.
*/
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=AnchorData)
FVector2D Alignment;
};

我们会发现,在面板中的 PositionSize,在源码中也是以 Offsets 形式存在的。

Anchor.Minimum.X / Anchor.Minimum.YAnchor.Maximum.X / Anchor.Maximum.YAlignment.X / Alignment.Y,均是以 比例 的形式给出。

布局规则

实际上,如果我们改动 Anchor,在 Minimum.X ≠ Maximum.X 的时候,Position X 会显示为 Offset LeftSize X 会显示为 OffsetRight

image-20210812164808737

在控件绝对位置不变的情况下,修改锚点的位置,会导致此时这两个值会改变非常多,对绝对位置的转换造成了困难。

接下来简单整理下其规则:

Minimum.X = Maximum.X Minimum.XMaximum.X
image-20210812160759667 image-20210812160922400
Alignment 有用:
Alignment.X * Size.X 确定了 “对齐点” 的横坐标偏移量。
Alignment.X 为 0 时,对齐点在最左边;
Alignment.X 为 1 时,对齐点在最右边;
Alignment 无用
采用:Position X
(表示 Anchor_Min 横坐标 到 对齐点.X 的距离)
image-20210812162308662
采用:Offset Left
(表示 Anchor_Min 横坐标 到 控件左侧的距离)
image-20210812163026182
采用:Size X
(表示控件自身的横坐标大小)
image-20210812162439904
采用 Offset Right
(表示 Anchor_Max 横坐标到 控件右侧的距离)
image-20210812163222501
AbsolutePosition.X = AnchorMinimumPositon.X + Offsets.Left - Alignment.X * Offsets.Right AbsolutePosition.X = MinimumPositon.X + Offsets.Left
AbsoluteSize.X = Offsets.Right AbsoluteSize.X = AnchorMaximumPositon.X - AnchorMinimumPositon.X - Offsets.Left - Offsets.Right

其中 AnchorMinimumPosition.X = Anchor.Minimum.X * 父控件的 Size.X

对于 Y 轴的处理也是类似的。Position Y 对应 Offset TopSize Y 对应 Offset Bottom