public class TestTextFieldFocusOnStartup : EW
{
[MenuItem("Test/Text Field Focus On Startup")]
public static void St()
{
var w = GetWindow<TestTextFieldFocusOnStartup>();
w.Show();
}
private void OnGUI()
{
EditorGUILayout.TextField("");
}
}
but I don’t know how to get TextField object and focus it on window show
update 2017-9-16: I find use EditorGUI.FocusTextInControl instead FocusControl will fix this question
public class TestTextFieldFocusOnStartup : EW
{
[MenuItem("Test/Text Field Focus On Startup")]
public static void St()
{
var w = GetWindow<TestTextFieldFocusOnStartup>();
w.Show();
}
private bool focused;
private void OnGUI()
{
GUI.SetNextControlName("prpr");
var text = EditorGUILayout.TextField("a text");
if (!focused)
{
EditorGUI.FocusTextInControl("prpr");
focused = true;
}
EditorGUILayout.TextField("");
}
}
update 2017-9-15:my old answer code will cause second open editorwindow focus input but cannot type, so I improve my code:
public class TestTextFieldFocusOnStartup : EW
{
[MenuItem("Test/Text Field Focus On Startup")]
public static void St()
{
var w = GetWindow<TestTextFieldFocusOnStartup>();
w.Show();
}
private bool focused;
private void OnGUI()
{
GUI.SetNextControlName("prpr");
EditorGUILayout.TextField("");
if (!focused)
{
GUI.FocusControl("prpr");
var te = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
te.cursorIndex = 1;
focused = true;
}
EditorGUILayout.TextField("");
}
}
old answer:
I find the solution
public class TestTextFieldFocusOnStartup : EW
{
[MenuItem("Test/Text Field Focus On Startup")]
public static void St()
{
var w = GetWindow<TestTextFieldFocusOnStartup>();
w.Show();
}
private void OnGUI()
{
GUI.SetNextControlName("prpr");
EditorGUILayout.TextField("");
GUI.FocusControl("prpr");
}
}