LoginSignup
4
1

More than 5 years have passed since last update.

【UnityEditor】EditorWindow上でキー入力を受け取る方法

Posted at

はじめに

EditorWindow上でのキー入力の検知、知らないと意外とはまるのでまとめてみました。

EditorWindow上でキー入力を検知するには

Event.current を見ることでキーが押されたかどうかを判別することができます。

ESCキーでEditorWindowを閉じてみる

実験として、ESCキーが押されたら閉じるEditorWindowを作ってみました。
ウィンドウにフォーカスが乗っている必要があります。

TestEditorWindow.cs
using UnityEngine;
using UnityEditor;

public class TestEditorWindow : EditorWindow
{
    [MenuItem("EditorWindow/TestEditorWindow")]
    static void Open()
    {
        GetWindow<TestEditorWindow>();
    }

    void OnGUI()
    {
        var e = Event.current;
        if (e.type == EventType.KeyDown && e.keyCode == KeyCode.Escape)
        {
            Debug.Log("Close");
            this.Close();
        }
    }
}

ESCキーを押すとウィンドウが閉じます。(完

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