- Home /
TextField not clearing
Does anyone have an idea why this code would result in me having the string hang around in the text field:
EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("+", GUILayout.Width(24.0f))) { if (m_CurEntry.m_Name.Length > 0) { SwitchSet.Switch newSwitch = new SwitchSet.Switch(); newSwitch.m_Name = m_CurEntry.m_Name; newSwitch.m_State = m_CurEntry.m_State; m_Set.m_Switches.Add(newSwitch);
m_CurEntry.m_Name = "";
m_CurEntry.m_State = false;
}
}
m_CurEntry.m_Name = EditorGUILayout.TextField(m_CurEntry.m_Name, GUILayout.Width(100.0f));
m_CurEntry.m_State = EditorGUILayout.Toggle(m_CurEntry.m_State);
EditorGUILayout.EndHorizontal();
Thanks
Just discovered that if I enter text, hit enter, then hit my + button the field clears correctly. It seems the text stays around only if I type the text and then hit the + button without pressing enter first.
I have a similar problem with text that is set procedurally not getting updated in the UI of my custom editor window. It's a filename in a text field, and if the cursor is in the text field when the user hits my browse-for-file button then the file they select is not displayed until user presses enter or clicks another control. Calling OnGUI() doesn't work, nor does Repaint(), nor Focus(), nor does setting GUI.changed = true.
Answer by drastick · Oct 29, 2012 at 08:36 AM
I was running into the same issue trying to clear a string in a TextField when I hit the clear button. The text would clear but the field would remain with the old text.
Here is my fix for to get it to clear, the trick is you need to set the next control name before FocusControl will work to force the text field out of focus.
nameFilter = EditorGUILayout.TextField("Name", nameFilter);
GUI.SetNextControlName("Clear");
if(GUILayout.Button("Clear", GUILayout.MaxWidth(70.0f)))
{
nameFilter = "";
GUI.FocusControl("Clear");
}
Answer by thienhaflash · Jan 19, 2013 at 11:16 AM
The textfield's content will only update when user input or change focus, so, you need to do it manually by setting GUIUtility.keyboardControl = 0;
This is the simplest solution and probably ought to be the accepted answer.
Answer by Bravini · Oct 20, 2010 at 07:03 PM
GUILayout works only on the OnGUI function that is drawn twice every frame. That is, if you want it to be shut down you need to code a condition for it to appear and switch it to off (an if statement will do the trick). http://unity3d.com/support/documentation/Components/GUI%20Scripting%20Guide.html
If your code is performance reliant I suggest you use GUIText instead, then your code would work on GUIText.enabled =false.
I'm sorry - I should have clarified that I still want the control to remain visible, just the text to be cleared. I use the text field to allow for a new entry to be added, and the button takes what's in the field and adds it to a list and then clears the text field so it's ready for new text to be entered. I hope this is making sense and I'm not wasting anyone's time.
Thank you so much for your response!
Your answer
Follow this Question
Related Questions
EditorGUILayout textfield return value problem 1 Answer
Fix editor TextField cursor alignment 1 Answer
How can I determine when user presses enter in an editor text field? 3 Answers
EditorGUILayout.TextField returning empty string 3 Answers
How to update a EditorGUILayout Textfield value in realtime? 0 Answers