system
October 20, 2010, 6:08pm
1
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
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");
}
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).
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.