- 在控件蓝图中添加容器,注意命名不要有中文
-
C++代码中找到容器实例
1
2
3
4
5
6
7
8
UVerticalBox* verticalBox = Cast<UVerticalBox>(CurrentWidget->GetWidgetFromName(TEXT(
"VerticalBox_0"
)));
if
(verticalBox != nullptr)
{
UScrollBox* ScrollBox = Cast<UScrollBox>(CurrentWidget->GetWidgetFromName(TEXT(
"ScrollBox_0"
)));
}
else
{
UE_LOG(LogTemp, Warning, TEXT(
"verticalBox is not find"
));
}
-
创建Button实例,设置标题,绑定点击事件
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
UVRVIUButton* Button = NewObject<UVRVIUButton>(
this
);
UTextBlock* ButtonText = NewObject<UTextBlock>(
this
);
ButtonText->SetJustification(ETextJustify::Left);
ButtonText->SetColorAndOpacity(FSlateColor(FLinearColor::Black));
ScrollBox->AddChild(Button);
UPanelSlot* Slot = Button->Slot;
if
(Slot)
{
UCanvasPanelSlot* ButtonSlot = Cast<UCanvasPanelSlot>(Slot);
if
(ButtonSlot) {
ButtonSlot->SetAutoSize(
true
);
}
}
//原始Button事件绑定,项目中用到的是自定义Button
//Button->OnClicked.AddDynamic(this, &ALandMasterGameMode::OnButtonClicked1);
//Button->OnClicked.AddDynamic(Button, &UVRVIUButton::HandleButtonClicked);
Button->OnVRVIUButtonClicked.AddDynamic(
this
, &ALandMasterGameMode::OnButtonClicked);
FString btnText = FString::Printf(TEXT(
"%s-%s"
), *itemHolder->name, *itemHolder->intranet_ip);
ButtonText->SetText(FText::FromString(btnText));
Button->AddChild(ButtonText);
Button->ButtonID = itemHolder->room_id;
Button->AdditionalParameter = itemHolder->intranet_ip;
UButtonSlot* ButtonSlot = Cast<UButtonSlot>(ButtonText->Slot);
if
(ButtonSlot) {
ButtonSlot->SetPadding(FMargin(0.f, ButtonSlot->Padding.Top,0.f, ButtonSlot->Padding.Bottom));
}
-
创建点击事件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void
ALandMasterGameMode::OnButtonClicked(int32 ButtonID, FString AdditionalParameterr) {
UE_LOG(LogTemp, Warning, TEXT(
"On Button Clicked roomId is %d, ipAdress is %s"
), ButtonID, *AdditionalParameterr);
this
->IntranetIP = AdditionalParameterr;
if
(nullptr != CurrentWidget)
{
//通过代码调用Button点击事件
UButton* btn = Cast<UButton>(CurrentWidget->GetWidgetFromName(TEXT(
"ConnectButton"
)));
if
(btn != nullptr)
{
btn->OnClicked.Broadcast();
}
else
{
UE_LOG(LogTemp, Warning, TEXT(
"verticalBox is not find"
));
}
}
}