接上篇,本篇使用第二种方法来访问UMG中Widget变量,主要使用的方法是添加标签:
UPROPERTY(meta = (BindWidget))
步骤
第一步
添加继承于UUserWidget的C++类
头文件:SessionRow.h
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "SessionRow.generated.h"
/**
*
*/
UCLASS()
class STEAMDEMO_API USessionRow : public UUserWidget
{
GENERATED_BODY()
public:
UPROPERTY(meta = (BindWidget))
class UTextBlock* ServerName;
UPROPERTY(meta = (BindWidget))
class UTextBlock* HostUser;
UPROPERTY(meta = (BindWidget))
class UTextBlock* ConnectionFraction;
UPROPERTY(BlueprintReadOnly)
bool Selected = false;
void Setup(class UMainMenu* Parent, uint32 Index);
private:
UPROPERTY(meta = (BindWidget))
class UButton* RowButton;
UPROPERTY()
class UMainMenu* Parent;
uint32 Index = -1;
UFUNCTION()
void OnClicked();
};
cpp文件:SessionRow.cpp
#include "SessionRow.h"
#include "MainMenu.h"
#include "Components/Button.h"
#include "Components/TextBlock.h"
void USessionRow::Setup(UMainMenu* InParent, uint32 InIndex)
{
Parent = InParent;
Index = InIndex;
RowButton->OnClicked.AddDynamic(this, &USessionRow::OnClicked);
}
void USessionRow::OnClicked()
{
if (Parent == nullptr) return;
UE_LOG(LogTemp, Warning, TEXT("[USessionRow::OnClicked] Index %i"), Index);
Parent->SelectIndexSessionList(Index);
}
第二步
创建Widget蓝图类WBP_SessionRow,继承于第一步创建的C++类Session Row
注意点:Widget蓝图类中的控件名称需要与C++类中的变量名保持一致才能绑定上。
?
|