0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【Unityエディター拡張】EditorWindow表示時にTextFieldにフォーカスをのせる

Last updated at Posted at 2016-07-11

※この記事のUnityのバージョンは5.3.5f1です

はじめに

EditorWindowを表示させるとき、最初からTextFieldにフォーカスをのせておきたい場合があります。
その方法を調べてみました。

OnGUIの中で EditorGUI.FocusTextInControl() を呼ぶとTextFieldにフォーカスが乗るみたいです。

検証用ソースコード

TestWindow.cs
# if UNITY_EDITOR
using UnityEngine;
using UnityEditor;

public class TestWindow : EditorWindow
{
    const string NAME = "A";

    string text = "ほげ";
    int cnt = 0;

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

    void OnGUI()
    {
        text = EditorGUILayout.TextField("Text 1", text);

        GUI.SetNextControlName(NAME);
        text = EditorGUILayout.TextField("Text 2", text);

        text = EditorGUILayout.TextField("Text 3", text);

        if (cnt == 0)
        {
            // TextFieldへフォーカスをのせる
            EditorGUI.FocusTextInControl(NAME);
        }
        cnt++;
    }
}
# endif //UNITY_EDITOR

実行結果

上記のEditorWindowを開くと、はじめからTextFieldにフォーカスが乗っていることが確認できます。
image

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?