目录
找了好多的解决办法,都不管用。这个算是折中的。
需求
将当前的用户控件(ui)截图下来,并赋值到一个texture2d上。
我的需求:文字游戏中,点击存档,将当前的游戏画面转换为一个纹理2d,任何将纹理2d赋值到该存档的头图。

思路
先截图到本地,然后再从本地读取成texture2d。
1.截图并读取到本地
参考:
cpp
.h
UFUNCTION(BlueprintCallable, Category = "MyFunc", meta = (Keywords = "ScreenShot_"))
void CaptureUIScreenshot(UUserWidget* Widget ,const FString& SavePath);
.cpp
void UMyScreenShotComponent::CaptureUIScreenshot(UUserWidget* Widget, const FString& SavePath)
{
TSharedRef<SWidget> TargetSWidget = Widget->TakeWidget();
TArray<FColor> RawData;
FIntVector OutSize;
bool bScreenshotSuccessful = FSlateApplication::Get().TakeScreenshot(TargetSWidget, RawData, OutSize);
if (bScreenshotSuccessful) {
TArray<uint8> CompressedBitmap;
FImageUtils::CompressImageArray(OutSize.X, OutSize.Y, RawData, CompressedBitmap);
FFileHelper::SaveArrayToFile(CompressedBitmap, *SavePath);
}
}
2.本地读取图片并转换为纹理2d
插件


效果展示