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();
}
}
I experienced the same problem. Implemented what you say, i.e. added dummy textField, assigned it some constant name. I create it on each OnGUI. When my layout changes (and some controls go), I do GUI.FocusControl on this dummy textField. The textField is being focused. The problem remains. Clicking any of controls with new name doesn't make it returned from GetNameOfFocusedControl(). No change here. Can you please attach some code that fixed the issue for you?
– LancetedI have same problem and i think this is a bug. gui should look at controls that rendered in last OnGUI function not the history.
– OozeMan, this is a bad one! Has anybody filed this bug with Unity? I'd certainly vote for a fix... the workaround below is going to be hard to apply in my case.
– Zoology