Hello,
I learn Unreal (5.1.1), probably the hard way. I attempt to read pixels from screen and adjust brightness of my Widget components. For example if the widget is over a bright sky, then the widged/texts should be dark. I plan only two situations: dark texts or bright texts, depending on screen brightness.
In order to achieve this goal and for educational reasons, I wrote in C++ my widget’s parent class: as I included below.
The problem is that GetViewportScreenShot() seems to work correctly only under Unreal Editor, so that “MyPureMethodN” returns the Red level of a pixel (top left pixel on screen), depending on what is going on the screen.
Unoftunately when I pack my project (for Windows), then my MyPureMethodN() returns 0, as if the screenshot was not taken. However Bitmap[] is of size of the screen. Bitmap[0].R remains 0 unfortunately. Later I would read R/G/B and decide what is the brightness of the pixel. VS 2022 Community.
Please, I am 2-week beginner of Unreal, how to make GetViewportScreenShot() work correctly in Windows package not only under Unreal Editor??? Some hints, please.
MyUserWidgetN.cpp:
#include "MyUserWidgetN.h"
int UMyUserWidgetN::MyPureMethodN()
{
UGameViewportClient* Viewport = GetWorld()->GetGameViewport();
FIntPoint ViewSize = Viewport->Viewport->GetSizeXY();
FViewport* InViewport = Viewport->Viewport;
TArray<FColor> Bitmap;
FIntRect Rect(0, 0, InViewport->GetSizeXY().X, InViewport->GetSizeXY().Y);
bool bScreenshotSuccessful = GetViewportScreenShot(InViewport, Bitmap, Rect);
if (bScreenshotSuccessful)
{
return Bitmap[0].R;
}
return 100;
}
MyUserWidgetN.h:
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "MyUserWidgetN.generated.h"
UCLASS()
class VISUALSTUDIOTESTC_API UMyUserWidgetN : public UUserWidget
{
GENERATED_BODY()
public:
UFUNCTION(BlueprintCallable, BlueprintPure, Category = "MyCategory")
int MyPureMethodN();
};