Skip to content

Instantly share code, notes, and snippets.

@JonathanADaley
Last active September 11, 2023 06:57
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save JonathanADaley/151f26b145981336371b73def45209e7 to your computer and use it in GitHub Desktop.
Save JonathanADaley/151f26b145981336371b73def45209e7 to your computer and use it in GitHub Desktop.
How to get the DPI Scale of UMG at runtime in Unreal Engine 4 C++
// this function requires the UserInterfaceSettings header to be included
#include Runtime/Engine/Classes/Engine/UserInterfaceSettings.h
// this function can be marked as Blueprint Pure in its declaration, as it simply returns a float
float MyBPFL::GetUMG_DPI_Scale() {
// need a variable here to pass to the GetViewportSize function
FVector2D viewportSize;
// as this function returns through the parameter, we just need to call it by passing in our FVector2D variable
GEngine->GameViewport->GetViewportSize(viewportSize);
// we need to floor the float values of the viewport size so we can pass those into the GetDPIScaleBasedOnSize function
int32 X = FGenericPlatformMath::FloorToInt(viewportSize.X);
int32 Y = FGenericPlatformMath::FloorToInt(viewportSize.Y);
// the GetDPIScaleBasedOnSize function takes an FIntPoint, so we construct one out of the floored floats of the viewport
// the fuction returns a float, so we can return the value out of our function here
return GetDefault<UUserInterfaceSettings>(UUserInterfaceSettings::StaticClass())->GetDPIScaleBasedOnSize(FIntPoint(X,Y));
}
@JonathanADaley
Copy link
Author

Shoutout to Unreal AnswerHub user "Elringus" as they posted an example of using the Engine APIs required to write this function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment