【Unity】【エディタ拡張】Game View の解像度を取得する 
2023/09/02 Sat [edit]
ランタイム時の画面解像度は (Screen.width, Screen.height) で取得できるのだが、エディタスクリプト上で Screen で画面解像度を取得すると、よくわからない値になる。

調べてみたら、UnityStats.screenRes というプロパティ(Unityマニュアルではなぜか見つからない)で取得できるとあったので、いつものように静的な関数として定義しておくと便利だと思った。簡単なサンプルを載せておこう。
(※) Unity 2020.3.34f1 / Windows11(x64) で確認
●Game View の解像度を取得する (戻値:Vector2Int 型)
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
public static partial class Utils //※クラス名は任意
{
/// <summary>
/// Game View の解像度を取得する (戻値:Vector2Int 型)
/// 2023/09/02 Fantom
/// http://fantom1x.blog130.fc2.com/blog-entry-431.html
/// </summary>
/// <returns>x = width, y = height</returns>
public static Vector2Int GetGameViewResolution()
{
var res = UnityStats.screenRes.Split('x'); //"1920x1080" 等
return new Vector2Int(int.Parse(res[0]), int.Parse(res[1]));
}
}
#endif
●エディタスクリプトからの使用例 (戻値:Vector2Int 型)
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(GameViweResoTest))] //クラス名は任意(ヒエラルキーにアタッチ)
public class GameViweResoTestEditor : Editor //クラス名は任意(※Editorフォルダに置く)
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
GUILayout.Space(15);
if (GUILayout.Button("GameViewResolution"))
{
Debug.Log($"Screen: {Screen.width}x{Screen.height}");
var reso = Utils.GetGameViewResolution();
Debug.Log($"GameViewResolution: {reso.x}x{reso.y}");
}
GUILayout.Space(15);
}
}
戻値に Vector2Int を使ってるが、x が width, y が height となる。
ここでは MonoBehaviour を継承した適当なスクリプト(GameViweResoTest.cs)を作成し、ヒエラルキーにアタッチしている。
1280x720, 1920x1080 等、定番の解像度を使ってるが、「Free Aspect」の状態でも取得できるようだ。そのときに 425x776 など、実際の画面上の解像度(?)になるらしい。

●結果例 (GameView: 4K UHD)
GameViewResolution: 3840x2160
GameViewResolution (Tuple): 3840x2160
現在の Unityバージョン(掲載時点:Unity2020)だと C# も Tuple 型が使えるので(必要なら、.Net 4.x にする)、戻値を Tuple 型にしておくと、少しばかり簡潔になるので良いかもしれない。
●Game View の解像度を取得する (戻値:Tuple 型)
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
public static partial class Utils //※クラス名は任意
{
/// <summary>
/// Game View の解像度を取得する (戻値:Tuple 型)
/// 2023/09/02 Fantom
/// http://fantom1x.blog130.fc2.com/blog-entry-431.html
/// </summary>
/// <returns>w = width, h = height</returns>
public static (int w, int h) GetGameViewResolutionAsTuple()
{
var res = UnityStats.screenRes.Split('x'); //"1920x1080" 等
return (int.Parse(res[0]), int.Parse(res[1]));
}
}
#endif
●エディタスクリプトからの使用例 (戻値:Tuple 型)
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(GameViweResoTest))] //クラス名は任意(ヒエラルキーにアタッチ)
public class GameViweResoTestEditor : Editor //クラス名は任意(※Editorフォルダに置く)
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
GUILayout.Space(15);
if (GUILayout.Button("GameViewResolution"))
{
Debug.Log($"Screen: {Screen.width}x{Screen.height}");
var (w, h) = Utils.GetGameViewResolutionAsTuple();
Debug.Log($"GameViewResolution (Tuple): {w}x{h}");
}
GUILayout.Space(15);
}
}
注意点として、エディタスクリプトは任意の「Editor」フォルダ内に入れて使うか、プリプロセッサ(#if UNITY_EDITOR~#endif)で囲んで使う(通常はヒエラルキーでアタッチするスクリプトはプリプロセッサで囲み、エディタのみ使うものは Editor フォルダに入れるのが簡単)。これらは Unityエディタ上では使えるが、ランタイムでは使えない。あくまで開発支援用スクリプトとなる。
自由に改造して使うと良い。
(関連記事)
【Unity】【C#】画面解像度とアクペクト比(整数)を求める
【Unity】【C#】指定ワールド位置がカメラに映っているか調べる
【Unity】固定背景画像(2D)を表示する
- 関連記事
トラックバック
トラックバックURL
→http://fantom1x.blog130.fc2.com/tb.php/431-a64cff96
この記事にトラックバックする(FC2ブログユーザー)
| h o m e |