繁体   English   中英

虚幻引擎 c++ 写委托 OnScreenshotCaptured

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM