LoginSignup
0
1

More than 5 years have passed since last update.

【UnityEditor】EditorGUILayout.TextArea入力時にESCキーを押すと入力内容が消えてしまう問題の解決

Posted at

はじめに

EditorGUILayout.TextAreaでテキストを入力している時にESCキーを押すと入力内容が消えてしまいます。

ESCキー押下時に入力内容を確定させるにはどうすればいいのか調べてみました。

Unityのバージョンは5.5.0b7です

GUI.FocusControlを使う

ESCキーが押されたとき、GUI.FocusControl("")を呼んでGUIのフォーカスを外してやると入力内容が消えずに済むみたいです。

検証用ソースコード

TestEditorWindow.cs
using UnityEngine;
using UnityEditor;

public class TestEditorWindow : EditorWindow 
{
    private string text = "";

    [MenuItem("Test/TestEditorWindow")]
    static void Open()
    {
        GetWindow<TestEditorWindow>();
    }   

    void OnGUI()
    {
        if (Event.current.keyCode == KeyCode.Escape) { GUI.FocusControl(""); }
        this.text = EditorGUILayout.TextArea(this.text);
    }
}

上記スクリプトをEditorフォルダに入れてください。

検証

TextAreaにテキストを入力して、そのままESCキーを入力してみます。

GUI.FocusControl("") を呼ばない場合

ESCキーを押すと入力内容が消えてしまいます。

GUI.FocusControl("") を呼んだ場合

ESCキーを押しても入力内容が消えなくなりました(完)

参考URL

GUIのフォーカスを外す - けいごのなんとか
http://anchan828.hatenablog.jp/entry/2013/10/10/161401

0
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
0
1