今回は「CameraComponentやPlayerCameraManagerのConstrain Aspect Ratioにチェック」を入れた際のゲーム画面サイズを取得する方法です。製作中の個人ゲームにて、これが必要になったので調べました。
残念ながらC++でしか解決方法が見つかりませんでしたがBPや関数ベースで取得する方法があれば出来る限り優しく自分に教えてあげてください。
さっそくソースコード
FVector2D GetGameViewSize(UObject* WorldContextObject) { APlayerController* pController = UGameplayStatics::GetPlayerController(WorldContextObject, 0); if (pController == nullptr) { return FVector2D::ZeroVector; } ULocalPlayer const* const LP = pController->GetLocalPlayer(); if (LP == nullptr || LP->ViewportClient == nullptr) { return FVector2D::ZeroVector; } FSceneViewProjectionData ProjectionData; LP->GetProjectionData(LP->ViewportClient->Viewport, eSSP_FULL, ProjectionData); return FVector2D( ProjectionData.GetConstrainedViewRect().Width(), ProjectionData.GetConstrainedViewRect().Height() ); }
このコードはUGameplayStatics::ProjectWorldToScreenを参考にして作りました。