Does anyone have an idea why this code would result in me having the string hang around in the text field:
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_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);
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);
nameFilter = "";
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;
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).
If your code is performance reliant I suggest you use GUIText instead, then your code would work on GUIText.enabled =false.