ゲーム作りが大好きな人のブログ

ゲームを作るのが大好きな人のブログ。UE4とBlender、MAYA(LT)、3DCoatを使用しています!

【UE4】【C++】アスペクト比を固定した際のゲーム画面サイズを取得する方法

f:id:toofu0:20201222164807p:plain

今回は「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を参考にして作りました。

その他参考文献

qiita.com