如上图所示,Slate控件主要分为三类:SCompoundWidget、SPanel、SLeafWidget,下面将一一讲解。
SCompoundWidget
SCompoundWidget对应于UMG中的“WidgetBlueprint(控件蓝图)”,用来作为控件容器。当我们在C++类向导中创建Slate类时,创建的就是SCompoundWidget。
可以通过成员ChildSlot结合重载的[]操作符往控件里面添加其他控件,例如教程1中:
void SStandardSlateWidget::Construct(const FArguments& InArgs)
{
ChildSlot
[
SNew(STextBlock)
.Font(FSlateFontInfo("Veranda", 100))
.Text(NSLOCTEXT("HelloSlate", "HelloSlateText", "Hello, Slate!"))
];
}
此外,最常用的SButton控件也属于SCompoundWidget,可以使用Content函数结合重载的[]操作符添加其他控件:
void SStandardSlateWidget::Construct(const FArguments& InArgs)
{
ChildSlot
[
SNew(SButton)
.Content()
[
SNew(STextBlock)
.Font(FSlateFontInfo("Veranda", 100))
.Text(NSLOCTEXT("HelloSlate", "HelloSlateText", "Hello, Slate!"))
]
];
}
效果如下(白色区域是一个超大的SButton):
SPanel
SPanel对应于UMG中的Panel分组:
SPanel用来设置布局,使用Slot添加多个子控件(SCompoundWidget只能添加1个子控件)。那么何为Slot呢?Slot即为UMG中的插槽,假设UMG的层级如下:
选中“Button_49”,可以在其细节面板中看到:
即为SPanel子布局的属性,包括Padding、Size、Horizontal Align、Vertical Align。
使用Slot添加子控件可以使用“+ Slot()”,也可以调用AddSlot函数,还是以教程1中的SStandardSlateWidget为例:
void SStandardSlateWidget::Construct(const FArguments& InArgs)
{
ChildSlot
[
SAssignNew(VerticalBoxPtr, SVerticalBox)
+ SVerticalBox::Slot()
.Padding(1.0)
.FillHeight(0.3f)
.HAlign(HAlign_Fill)
.VAlign(VAlign_Top)
[
SNew(SButton)
]
+ SVerticalBox::Slot()
.FillHeight(0.5f)
.HAlign(HAlign_Center)
[
SNew(SButton)
]
];
VerticalBoxPtr->AddSlot()
.FillHeight(0.2f)
[
SNew(SButton)
];
}
效果如下:
SLeafWidget
叶子控件,故名思意,该控件不能添加子控件。
常用的为STextBlock与SImage。