For anyone wondering how it’s actually done in Unreal Engine 5, start by creating a custom renderer:
UCLASS(MinimalAPI)
class UMyThumbnailRenderer : public UThumbnailRenderer
{
GENERATED_BODY()
public:
// Begin UThumbnailRenderer Object
YOUREDITOR_API virtual void GetThumbnailSize(UObject* Object, float Zoom, uint32& OutWidth, uint32& OutHeight) const override;
YOUREDITOR_API virtual void Draw(UObject* Object, int32 X, int32 Y, uint32 Width, uint32 Height, FRenderTarget*, FCanvas* Canvas, bool bAdditionalViewFamily) override;
// End UThumbnailRenderer Object
};
In your module initialization function, register it:
void FMyditorModule::StartupModule()
{
UThumbnailManager::Get().RegisterCustomRenderer(UMyAsset::StaticClass(), UMyThumbnailRenderer::StaticClass());
}
void FMyEditorModule::ShutdownModule()
{
if (FModuleManager::Get().IsModuleLoaded("AssetTools"))
{
UThumbnailManager::Get().UnregisterCustomRenderer(UMyThumbnailRenderer::StaticClass());
}
}
For how to implement UMyThumbnailRenderer::Draw
, you might want to look how it’s done for textures in the Engine.
For example, to draw a centered text, you can use the following code:
static void DrawTextOnCanvas(const TCHAR* Text, uint32 Width, uint32 Height, FCanvas* Canvas, float Scale = 1.f, FLinearColor Color = FLinearColor::White)
{
int32 TextWidth = 0;
int32 TextHeight = 0;
StringSize(GEngine->GetLargeFont(), TextWidth, TextHeight, Text);
FCanvasTextItem TextItem(FVector2D((Width - TextWidth * Scale) / 2., (Height - TextHeight * Scale) / 2.), FText::FromString(Text), GEngine->GetLargeFont(), Color);
TextItem.EnableShadow(FLinearColor::Black);
TextItem.Scale = FVector2D::UnitVector * Scale;
TextItem.Draw(Canvas);
}