PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });
Zapisz zmiany, zrekompiluj projekt przy pomocy skrótu klawiaturowego Ctrl+Shift+B w VS i otwórz ponownie projekt w edytorze UE5.
CustomButton.h
#pragma once
#include "CoreMinimal.h"
#include "SlateCore/Public/Widgets/SCompoundWidget.h"
class TESTPROJECT_API SCustomButton : public SCompoundWidget
{
SLATE_BEGIN_ARGS(SCustomButton)
: _Label(TEXT("Click me!"))
, _ButtonClicked()
{}
SLATE_ATTRIBUTE(FString, Label)
SLATE_EVENT(FOnClicked, ButtonClicked)
SLATE_END_ARGS()
public:
void Construct(const FArguments& InArgs);
TAttribute Label;
FOnClicked ButtonClicked;
};
CustomButton.cpp
#include "CustomButton.h"
void SCustomButton::Construct(const FArguments& InArgs)
{
Label = InArgs._Label;
ButtonClicked = InArgs._ButtonClicked;
ChildSlot.VAlign(VAlign_Center)
.HAlign(HAlign_Center)
[
SNew(SButton)
.OnClicked(ButtonClicked)
.Content()
[
SNew(STextBlock)
.Text_Lambda([this] {return FText::FromString(Label.Get()); })
]
];
}
CustomButtonWidget.h
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "CustomButton.h"
#include "Slate/Public/Framework/SlateDelegates.h"
#include "CustomButtonWidget.generated.h"
DECLARE_DYNAMIC_DELEGATE_RetVal(FString, FGetString);
UCLASS()
class TESTPROJECT_API UCustomButtonWidget : public UUserWidget
{
GENERATED_BODY()
protected:
TSharedPtr MyButton;
virtual TSharedRef RebuildWidget() override;
public:
FReply OnButtonClicked();
UPROPERTY(BlueprintReadWrite, EditAnywhere)
FString Label;
UPROPERTY()
FGetString LabelDelegate;
virtual void SynchronizeProperties() override;
PROPERTY_BINDING_IMPLEMENTATION(FString, Label);
};
CustomButtonWidget.cpp
#include "CustomButtonWidget.h"
TSharedRef UCustomButtonWidget::RebuildWidget()
{
MyButton = SNew(SCustomButton)
.ButtonClicked(BIND_UOBJECT_DELEGATE(FOnClicked, OnButtonClicked));
return MyButton.ToSharedRef();
}
FReply UCustomButtonWidget::OnButtonClicked()
{
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Button Clicked"));
return FReply::Handled();
}
void UCustomButtonWidget::SynchronizeProperties()
{
if (Label.Equals("")) {
Label = TEXT("Click me!");
}
Super::SynchronizeProperties();
TAttribute LabelBinding = PROPERTY_BINDING(FString, Label);
MyButton->Label = LabelBinding;
}
SampleGameModeBase.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/GameModeBase.h"
#include "CustomButtonWidget.h"
#include "SampleGameModeBase.generated.h"
UCLASS()
class TESTPROJECT_API ASampleGameModeBase : public AGameModeBase
{
GENERATED_BODY()
UPROPERTY()
UCustomButtonWidget* CustomButton;
protected:
virtual void BeginPlay() override;
};
SampleGameModeBase.cpp
#include "SampleGameModeBase.h"
#include "Blueprint/UserWidget.h"
void ASampleGameModeBase::BeginPlay()
{
Super::BeginPlay();
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("SampleGameModeBase!"));
TSubclassOf ButtonClass = UCustomButtonWidget::StaticClass();
CustomButton = CreateWidget(GetWorld(), ButtonClass);
if (CustomButton != nullptr)
{
CustomButton->AddToViewport();
}
}