Moduleの組み込み
Playground51.Build.cs
PrivateDependencyModuleNames.AddRange(new string[] {"Slate", "SlateCore" });
Slateの宣言
SExample.h
#pragma once
#include "Widgets/SCompoundWidget.h"
class PLAYGROUND51_API SExampleWidget : public SCompoundWidget
{
SLATE_BEGIN_ARGS(SExampleWidget){}
SLATE_ARGUMENT(FText, LabelText)
SLATE_END_ARGS()
public:
void Construct(const FArguments &InArgs);
void SetText(FText NewText);
protected:
FText Text;
FSlateFontInfo Font;
TSharedPtr<STextBlock> TextBlock;
};
SExample.cpp
#include "SExampleWidget.h"
void SExampleWidget::Construct(const FArguments& InArgs)
{
bCanSupportFocus = true;
Text = InArgs._LabelText;
Font = FCoreStyle::Get().GetFontStyle(FName("EmbossedText"));
Font.Size = 48;
ChildSlot
[
SNew(SVerticalBox)
+SVerticalBox::Slot()
.VAlign(VAlign_Center)
.HAlign(HAlign_Center)
[
SAssignNew(TextBlock, STextBlock)
.Text(Text)
.Font(Font)
.ColorAndOpacity(FColor::Magenta)
]
];
}
void SExampleWidget::SetText(FText NewText)
{
Text = NewText;
TextBlock->SetText(Text);
}
その1: HUDで表示させる
SlateSpawnHUD.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/HUD.h"
#include "SlateSpawnHUD.generated.h"
UCLASS()
class PLAYGROUND51_API ASlateSpawnHUD : public AHUD
{
GENERATED_BODY()
protected:
virtual void BeginPlay() override;
TSharedPtr<class SExampleWidget> ExampleWidget;
public:
UFUNCTION(BlueprintCallable)
void ChangeText(FString NewText);
};
SlateSpawnHUD.cpp
#include "SlateSpawnHUD.h"
#include "SExampleWidget.h"
void ASlateSpawnHUD::BeginPlay()
{
Super::BeginPlay();
ExampleWidget = SNew(SExampleWidget).LabelText(FText::FromString("This is created by Slate"));
UGameViewportClient* ViewportClient = GetWorld()->GetGameViewport();
ViewportClient->AddViewportWidgetContent(ExampleWidget.ToSharedRef());
}
void ASlateSpawnHUD::ChangeText(FString NewText)
{
ExampleWidget->SetText(FText::FromString(NewText));
}
GameModeを作って前述のHUDを指定
実行して表示
おまけ
Level BlueprintでLボタンを押すとメッセージが変わるように設定
その2: UWidget(UUserWidget)でラップして表示
SlateWrapperWidget.h
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "SlateWrapperWidget.generated.h"
UCLASS()
class PLAYGROUND51_API USlateWrapperWidget : public UUserWidget
{
GENERATED_BODY()
protected:
virtual TSharedRef<SWidget> RebuildWidget() override;
virtual void ReleaseSlateResources(bool bReleaseChildren) override;
public:
UPROPERTY(BlueprintReadWrite, Category="Slate", meta=(ExposeOnSpawn="true"))
FString TextForDisplay;
UFUNCTION(BlueprintCallable)
void ChangeText(FString NewText) const;
private:
TSharedPtr<class SExampleWidget> SlateWidget;
};
SlateWrapperWidget.cpp
#include "SlateWrapperWidget.h"
#include "SExampleWidget.h"
TSharedRef<SWidget> USlateWrapperWidget::RebuildWidget()
{
SlateWidget = SNew(SExampleWidget).LabelText(FText::FromString(TextForDisplay));
return SlateWidget.ToSharedRef();
}
void USlateWrapperWidget::ChangeText(FString NewText) const
{
SlateWidget->SetText(FText::FromString(NewText));
}
void USlateWrapperWidget::ReleaseSlateResources(bool bReleaseChildren)
{
SlateWidget.Reset();
}
下記2つをオーバーライドするよ
protected:
virtual TSharedRef<SWidget> RebuildWidget() override;
virtual void ReleaseSlateResources(bool bReleaseChildren) override;