Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save TOLINSKI/6cb43d043bc13dd28975bd8831c38b97 to your computer and use it in GitHub Desktop.
Save TOLINSKI/6cb43d043bc13dd28975bd8831c38b97 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));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment