【问题标题】:Unreal engine c++ write delegate OnScreenshotCaptured虚幻引擎 c++ 编写委托 OnScreenshotCaptured
【发布时间】:2018-05-28 14:40:09
【问题描述】:

我是 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/

有人可以告诉我如何实现它以及您如何看待/知道如何实现它吗?

【问题讨论】:

    标签: c++ delegates unreal-engine4


    【解决方案1】:

    我有一个替代方案,没有委托,但是通过实现您自己的 UGameViewportClient(继承它)并覆盖 Draw() 函数,将 FRenderTarget::ReadPixel() 分配给您分配的某个缓冲区。

    我将展示基本代码,但不完整。

    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);
    }
    

    【讨论】:

    • 谢谢,我一会儿看看
    • 嗯,我真的需要向代表描述一下
    猜你喜欢
    • 2019-11-11
    • 2018-12-30
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    • 2015-10-12
    • 2020-09-14
    • 2016-08-07
    • 2022-06-28
    相关资源
    最近更新 更多