興味の源泉

自分が興味を起こせるようなネタを雑多に書き綴るブログ

【Unity】EditorWindow入門

EditorWindow入門

前回の続きでEditorWindowをもう少し掘り下げていきます。

nanami-yamato.hatenablog.com

実行中のみ表示する

実行中のみ表示したいということはよくあります。

そういう時には、EditorApplication.isPlaying の値を見て false だったらそこで return してその先の処理をしないようにすることで実現できます。

if (!EditorApplication.isPlaying)
{
    return;
}

テキストエリア

f:id:nanami_yamato:20200201231025p:plain

改行できるテキストボックスを利用したいときはこれを使います。

/// <summary>テキストエリアの編集用文字列 ←(宣言部)</summary>
private string textArea = string.Empty;

// テキストエリア ← OnGUIの中のコード
textArea = GUILayout.TextArea(textArea);

ポップアップ

f:id:nanami_yamato:20200201231100p:plain

選択式のポップアップを使いたいときはこれを使います。

// ポップアップ
string[] items = new string[] { "選択肢1", "選択肢2", "選択肢3" };
popupSelectIndex = EditorGUILayout.Popup(popupSelectIndex, items);

自動配置

f:id:nanami_yamato:20200201231119p:plain

GUILayout.HorizontalScopeを使うことで、その直後の{}内の表示物を水平方向に等分で配置してくれます。

垂直方向のGUILayout.VerticalScopeもあります。

using (new GUILayout.HorizontalScope())
{
    // テキストボックス
    textBox1 = GUILayout.TextField(textBox1);

    // テキストエリア
    textArea = GUILayout.TextArea(textArea);

    GUILayout.Label("テスト");

    GUILayout.Label("テスト2");
}

画像を表示

画像を表示するときはGUI.DrawTextureを使うのですが、座標を指定しないといけません。

なので、GUILayoutUtility.GetLastRect()でひとつ前の表示物のRectを取得し、その座標を使っています。

/// <summar>テクスチャ ←(宣言部)</summary>
private Texture2D texture = null;

// 画像表示 ← OnGUIの中のコード
if (texture != null)
{
    Rect rect = GUILayoutUtility.GetLastRect();
    GUI.DrawTexture(new Rect(rect.x, rect.y + rect.height + 10, texture.width, texture.height), texture);
}

画像表示した場合は、そこより下に続けて表示物を置いていく場合は一工夫が必要になります。

まとめ

今回の内容全てを反映させたのが以下です。

f:id:nanami_yamato:20200201231407p:plain

using UnityEngine;
using UnityEditor;

public class EditorWindowTest1 : EditorWindow
{
    /// <summary>テキストボックスの編集用文字列</summary>
    private string textBox1 = string.Empty;

    /// <summary>ラベル用文字列</summary>
    private string label2 = "ラベル2";

    /// <summary>ラベル用文字列</summary>
    private string label3 = "";

    /// <summary>ポップアップの選択中インデックス</summary>
    private int popupSelectIndex = 0;

    /// <summary>テキストエリアの編集用文字列</summary>
    private string textArea = string.Empty;

    /// <summar>テクスチャ</summary>
    private Texture2D texture = null;

    [MenuItem("Tests/テストツール/Test1")]
    private static void Create()
    {
        GetWindow<EditorWindowTest1>("テストウィンドウ");
    }

    /// <summary>
    /// 実行時に呼ばれる
    /// </summary>
    private void OnEnable()
    {
        // 画像読み込み Asset/Resources/green.png
        texture = Resources.Load<Texture2D>("green");
    }

    /// <summary>
    /// 描画
    /// </summary>
    private void OnGUI()
    {
        // 常に描画するものを描画
        alwaysDraw();

        // 描画開始チェック
        if (!checkDrawStart())
        {
            return;
        }

        // 描画開始チェックを通ったときに描画
        draw();
    }

    /// <summary>
    /// 描画するかチェック
    /// </summary>
    /// <returns></returns>
    private bool checkDrawStart()
    {
        // 実行中でなければ表示しない
        if (!EditorApplication.isPlaying)
        {
            return false;
        }

        return true;
    }

    /// <summary>
    /// 常に描画するもの
    /// </summary>
    private void alwaysDraw()
    {
        // ラベル
        GUILayout.Label("常に描画するラベル");
    }

    /// <summary>
    /// UIを描画
    /// </summary>
    private void draw()
    {
        using (new GUILayout.HorizontalScope())
        {
            // テキストボックス
            textBox1 = GUILayout.TextField(textBox1);

            // テキストエリア
            textArea = GUILayout.TextArea(textArea);

            GUILayout.Label("テスト");

            GUILayout.Label("テスト2");
        }

        // ボタン
        if (GUILayout.Button("ボタン"))
        {
            label2 = "ボタン押された。 Text:" + textBox1 + "\nTextArea:" + textArea;
        }

        // ラベル表示
        GUILayout.Label(label2);

        // ポップアップ
        string[] items = new string[] { "選択肢1", "選択肢2", "選択肢3" };
        popupSelectIndex = EditorGUILayout.Popup(popupSelectIndex, items);


        // ポップアップを選択していたら選択した文字列をラベルに追加
        if (popupSelectIndex > 0)
        {
            label3 = items[popupSelectIndex];
            GUILayout.Label(label3);
        }

        // 画像表示
        if (texture != null)
        {
            Rect rect = GUILayoutUtility.GetLastRect();
            GUI.DrawTexture(new Rect(rect.x, rect.y + rect.height + 10, texture.width, texture.height), texture);
        }
    }
}

OnEnable関数でテクスチャを読み込んでいます。

OnGUI関数は「常に描画するもの」「実行時チェック」「実行中の描画」でそれぞれ関数化し流れを追いやすくしました。

Editor拡張でのツール作成や利便性を上げるのは大事なことです。

まずは一歩、この記事が何かのきっかけになれば良いなと思いながら締めとさせていただきます。