Slate friends! I Found a solution. There was an error when I was entering the path. If you only add TEXT(“/Game”) withing the StartupModule, you will get a “Assertion failed” (with UE 5.2). You need to add the full path
Module cpp:
#include "MyGame.h"
#include "UserInterface/MenuStyles.h"
#include "Modules/ModuleManager.h"
IMPLEMENT_PRIMARY_GAME_MODULE(FMyGameModule, MyGame, "MyGame");
void FMyGameModule::StartupModule()
{
FMenuStyles::Initialize(TEXT("MyCustomStyles"), TEXT("/Game/UserInterface/Styles/"));
}
void FMyGameModule::ShutdownModule()
{
FMenuStyles::Shutdown();
}
Module header:
#pragma once
#include "CoreMinimal.h"
class FMyGameModule : public FDefaultGameModuleImpl
{
public:
void StartupModule() override;
void ShutdownModule() override;
};
Then create a new class, I named it FMenuStyles. Header:
#include "CoreMinimal.h"
#include "Styling/SlateStyle.h"
class ISlateStyle;
class FMenuStyles
{
public:
static void Initialize(FString inStyleSetName, FString inResourcePath);
static void Shutdown();
static void ReloadTextures();
static const ISlateStyle& Get();
protected:
static TSharedRef<class FSlateStyleSet> Create(FString inStyleSetName, FString inResourcePath);
static TSharedPtr<class FSlateStyleSet> SimpleStyleInstance;
};
cpp:
#include "UserInterface/MenuStyles.h"
#include "Modules/ModuleManager.h"
#include "Styling/SlateStyleRegistry.h"
#include "Styling/SlateTypes.h"
#include "Styling/CoreStyle.h"
#include "Framework/Application/SlateApplication.h"
#include "Slate/SlateGameResources.h"
TSharedPtr<FSlateStyleSet > FMenuStyles::SimpleStyleInstance = NULL;
void FMenuStyles::Initialize(FString inStyleSetName, FString inResourcePath)
{
if (!SimpleStyleInstance.IsValid()) {
SimpleStyleInstance = Create(inStyleSetName, inResourcePath);
FSlateStyleRegistry::RegisterSlateStyle(*SimpleStyleInstance);
}
}
void FMenuStyles::Shutdown()
{
FSlateStyleRegistry::UnRegisterSlateStyle(*SimpleStyleInstance);
ensure(SimpleStyleInstance.IsUnique());
SimpleStyleInstance.Reset();
}
void FMenuStyles::ReloadTextures()
{
FSlateApplication::Get().GetRenderer()->ReloadTextureResources();
}
const ISlateStyle& FMenuStyles::Get()
{
return *SimpleStyleInstance;
}
TSharedRef<class FSlateStyleSet> FMenuStyles::Create(FString inStyleSetName, FString inResourcePath)
{
TSharedRef<FSlateGameResources> StyleRef = FSlateGameResources::New(FName(inStyleSetName), inResourcePath, inResourcePath);
return StyleRef;
}
Now you can create your widget slate style in the editor. Start up your game, right click in the content browser->User Interface->Slate Widget Style (you can choose brush or text as well). Enter the parameters for your style.
In you SCompoundWidget you can now get your styles by referencing their name. Notice that the path was added in the module, not here. That was the bug I was talking about at the top of the post.
SNew(SBorder).BorderImage(FMenuStyles::Get().GetBrush(TEXT("SB_DoorIcon")))
SNew(SScrollBox).Style(FMenuStyles::Get(), TEXT("SWS_ScrollBox"))
Remember to add the includes in the header
#include "UserInterface/MenuStyles.h"
I found this code in a YouTube tutorial, but he seem to have made it private now. It might get visible in the future “Unreal Engine UI Course about Slates”