Hi,
I would like to make a different `TextField` appear in the GUI depending on the selected `Button`. My problem is that when I click in the displayed TextField, the function `GUI.GetNameOfFocusedControl()` seems confused and can not tell which TextField is currently active. The function seems to erroneously remember the TextField that was previously displayed and returns its name instead of the name of the currently selected TextField.
Here is my code:
public class DynamicGUITest: MonoBehaviour {
private int choosenMenu = 0;
private GUIContent[] listButtons;
void Start ()
{
listButtons = new GUIContent[2];
listButtons[0] = new GUIContent("menu 1");
listButtons[1] = new GUIContent("menu 2");
}
void OnGUI()
{
//menu selection
GUILayout.BeginArea(new Rect(0, 20, 100, 200));
GUILayout.BeginVertical();
choosenMenu = GUILayout.SelectionGrid(choosenMenu, listButtons, 1);
GUILayout.EndVertical();
GUILayout.EndArea();
//menu
GUILayout.BeginArea(new Rect(140, 20, 100, 200));
GUILayout.BeginVertical();
if (choosenMenu == 0)
{
GUI.SetNextControlName("field0");
GUILayout.TextField("field0");
}
else
{
GUI.SetNextControlName("field1");
GUILayout.TextField("field1");
}
GUILayout.Label(GUI.GetNameOfFocusedControl() + " has focus");
//Error : always shows "field0 has focus" even when field1 has focus
GUILayout.EndVertical();
GUILayout.EndArea();
}
}