広島、晴れのち晴れ

【Unity】画面サイズを取得する

アスペクト比の違いで生じる画面サイズを取得します。

基本設定

Canvasにある「Canvas Scaler」の設定を下記のようにしています。

サイズ取得

[ Canvas Scaler ] → Match => Height(1)で設定している場合


float defaultAspect = 1920f / 1080f;
float currentAspect = Screen.width / (float)Screen.height;
float ratio = currentAspect / defaultAspect;

int maxScreenWidth = Mathf.RoundToInt((1920f * ratio);
int maxScreenHeight = 1080; // Heightは変わりません

[ Canvas Scaler ] → Match => Width(0)で設定している場合


float defaultAspect = 1080 / (float)1920;
float currentAspect = Screen.height / (float)Screen.width;
float ratio = currentAspect / defaultAspect;

int maxScreenWidth = 1920; // Widthは変わりません
int maxScreenHeight = Mathf.RoundToInt(1080f * ratio);
モバイルバージョンを終了