[英]Unreal engine c++ write delegate OnScreenshotCaptured
我是 C++ 的新手。 我已經用 C# 和 PHP 編寫了代碼。因為我使用的是 Unreal 引擎,所以我正在嘗試學習 C++。 對於我的項目,我需要在游戲中制作屏幕截圖並立即顯示,因此我想將其作為紋理獲取。
我制作了一個藍圖節點,它調用了我制作的這個函數:
void UMyBlueprintFunctionLibrary::TakeScreenshot()
{
FScreenshotRequest::RequestScreenshot(true);
if (GEngine)
GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, "Tried to take screenshot");
}
當我將鼠標懸停在 RequestScreenshot 上方時,我會看到以下彈出窗口:
“可以通過訂閱視窗 OnScreenshopCaptured 委托從內存中讀取屏幕截圖”
所以這就是我嘗試做的,但我不知道我是如何查找的: https : //docs.unrealengine.com/latest/INT/API/Runtime/Engine/Engine/UGameViewportClient/OnScreenshotCaptured/
有人能告訴我如何實現這一點以及你如何看待/知道如何實現它嗎?
我有一個替代方案,沒有委托,但是FRenderTarget::ReadPixel()
到你分配的一些緩沖區,通過實現你自己的UGameViewportClient
(繼承它),並覆蓋Draw()
函數。
我將展示基本代碼,但不完整。
void UMyGameViewportClient::Draw(FViewport* Viewport, FCanvas* SceneCanvas)
{
Super::Draw(Viewport, SceneCanvas);
if (any_condition_you_need) {
CaptureFrame();
}
}
void UMyGameViewportClient::CaptureFrame()
{
if (!Viewport) {
return;
}
if (ViewportSize.X == 0 || ViewportSize.Y == 0) {
return;
}
ColorBuffer.Empty(); // Declare this in header as TArray<FColor>
if (!Viewport->ReadPixels(ColorBuffer, FReadSurfaceDataFlags(),
FIntRect(0, 0, ViewportSize.X, ViewportSize.Y)))
{
return;
}
SaveThumbnailImage();
}
void UMyGameViewportClient::SaveThumbnailImage()
{
IImageWrapperModule& wrappermodule = FModuleManager::LoadModuleChecked<IImageWrapperModule>(FName("ImageWrapper"));
auto wrapper_ptr = wrappermodule.CreateImageWrapper(EImageFormat::PNG);
for (int i = 0; i < ColorBuffer.Num(); i++)
{
auto ptr = &ColorBuffer[i];
auto r = ptr->R;
auto b = ptr->B;
ptr->R = b;
ptr->B = r;
ptr->A = 255;
} // not necessary, if you like bgra, just change the following function argument to ERGBFormat::BGRA
wrapper_ptr->SetRaw(&ColorBuffer[0], ColorBuffer.Num() * 4,
ViewportSize.X, ViewportSize.Y, ERGBFormat::RGBA, 8);
FFileHelper::SaveArrayToFile(wrapper_ptr->GetCompressed(), *ThumbnailFile);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.