Get screen aspect ratio


How can I get viewport aspect ratio from Lua or C++ code? I’ve tried:

AzToolsFramework::ViewportInteraction::CameraState result;
AzToolsFramework::ViewportInteractionRequestBus::BroadcastResult(result, &AzToolsFramework::ViewportInteractionRequests::GetCameraState);
return result.m_viewportSize.GetX() / result.m_viewportSize.GetY();

but it only works for editor. I’ve also tried:

AZ::EntityId cameraId;
float width;
float height;
Camera::CameraRequestBus::EventResult(width, cameraId, &Camera::CameraComponentRequests::GetFrustumWidth);
Camera::CameraRequestBus::EventResult(height, cameraId, &Camera::CameraComponentRequests::GetFrustumHeight);
return width / height;

but it always returns 1 (regardless of actual size).

Thank you for advice,


Hi @Sasha,

There are a few different ways to do this depending on your context.

The way that the LyShine UI system gets the viewport width and height is using:

gEnv->pRenderer->GetOverlayWidth() and gEnv->pRenderer->GetOverlayHeight()

If you have a UI canvas already you can call GetCanvasSize on the canvas, this will give you the viewport size.

Hope that helps,


Thank you, works perfectly.