LoginSignup
4
1

More than 5 years have passed since last update.

【Unity拡張】Gameウィンドウの画面サイズを表示するEditorWindowを作ってみた

Last updated at Posted at 2018-01-31

はじめに

UIを調整している時、Gameウィンドウのサイズを確認したくなったので作ってみました。

作ったものについて

Gameウィンドウの画面サイズとアスペクト比を表示します。

1.gif

環境

Windows 10
Unity2017.2.0f3

ソースコード

UnityプロジェクトにEditorフォルダを作り、以下のスクリプトをフォルダの中へ入れてください

AspectRatioViewer.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class AspectRatioViewer : EditorWindow
{
    [MenuItem("Tools/AspectRatio Viewer")]
    static void Open()
    {
        GetWindow<AspectRatioViewer>(); // ウィンドウを開く
    }

    void OnGUI()
    {
        EditorGUILayout.LabelField("画面解像度を表示します");

        var sizes = UnityStats.screenRes.Split('x');
        var w = float.Parse(sizes[0]); // 横のサイズをもとめる
        var h = float.Parse(sizes[1]); // 縦のサイズをもとめる

        this.ShowValue("Width", w.ToString());
        this.ShowValue("Height", h.ToString());
        this.ShowValue("Aspect Ratio", string.Format("{0} : {1}", 1f, h / w));
    }

    void Update()
    {
        this.Repaint(); // ウィンドウにフォーカスが乗っていないときでも画面描画を更新
    }

    void ShowValue(string label, string value)
    {
        EditorGUILayout.BeginVertical("Box");
        EditorGUILayout.LabelField(label);
        EditorGUI.indentLevel++;
        EditorGUILayout.LabelField(value.ToString());
        EditorGUI.indentLevel--;
        EditorGUILayout.EndVertical();
    }
}

使う

メニューの Tools/AspectRatio Viewer を選択するとウィンドウが開きます。

image.png

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1