- UE系统Button点击事件无法传递参数,通过子类化系统Button添加自定义参数扩展实现Button点击事件参数传递
- 点击C++类文件夹,在右边的区域点击鼠标右键,在弹出的菜单中选择“新建C++类”
- 在弹出的菜单中选中“显示所有类”,选择Button作为父类, 点击“下一步”按钮
- 输入类名"UVRVIUButton",选择工程目录作为新类的目标模块,点击“公共”选择器,点击“创建类”按钮
-
声明点击事件回调方法
1
DECLARE_DYNAMIC_MULTICAST_DELEGATE_TwoParams(FOnVRVIUButtonClicked, int32, ButtonID, FString, AdditionalParameter);
-
定义点击事件回调变量
1
2
3
// 点击事件回调
UPROPERTY(BlueprintAssignable, Category =
"VRVIU Button"
)
FOnVRVIUButtonClicked OnVRVIUButtonClicked;
-
定义扩展参数变量
1
2
3
4
5
6
7
// 设置按钮ID
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"VRVIU Button"
)
int32 ButtonID;
// 设置按钮扩展参数
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category =
"VRVIU Button"
)
FString AdditionalParameter;
-
绑定按钮点击事件
1
2
// 设置按钮点击事件绑定
OnClicked.AddDynamic(
this
, &UVRVIUButton::HandleButtonClicked);
-
点击事件中调用回调方法,传递扩展参数
1
2
3
UE_LOG(LogTemp, Warning, TEXT(
"UVRVIUButton::HandleButtonClicked"
));
// 触发自定义按钮点击事件
OnVRVIUButtonClicked.Broadcast(ButtonID, AdditionalParameter);
-
调用者绑定回调事件
1
Button->OnVRVIUButtonClicked.AddDynamic(
this
, &ALandMasterGameMode::OnButtonClicked);
-
调用者实现回调事件,获取扩展参数
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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)
{
UButton* btn = Cast<UButton>(CurrentWidget->GetWidgetFromName(TEXT(
"ConnectButton"
)));
if
(btn != nullptr)
{
btn->OnClicked.Broadcast();
}
else
{
UE_LOG(LogTemp, Warning, TEXT(
"verticalBox is not find"
));
}
}
}