Hi.
I have complex GUI and there’re few textfields placed horizontally. Some of them could be hidden, and then shown again. So I found strage behaviour: control get focus and works fine, but it’s focus name wrong (it has name of hidden control).
I suppose it’s Unity bug. Anyone have ideas or suggestions?
Here is test code to show the problem.
Steps to reproduce:
- Show all textfields
- Hide 1st
- Focus 2rd
You can see: selected 2nd control, but FocusControlName is 1st.
Code:
using UnityEngine;
using System.Collections;
public class TextFieldBugTest : MonoBehaviour {
string[] text = new string[] { "text0", "text1", "text2" };
bool[] show = new bool[] { true, true, true };
void OnGUI() {
// show textfields
for (int i = 0; i < text.Length; i++) {
if (show[i]) {
GUILayout.BeginHorizontal();
// set control name
GUI.SetNextControlName( "" + i );
// show actually textfield
// 'my control name' just to be sure, that it's correct control.
text[ i ] = GUILayout.TextField( text[ i ] );
GUILayout.Label ( "Control name " + i );
GUILayout.EndHorizontal();
}
}
//-------------------
// functional buttons
for (int i = 0; i < text.Length; i++) {
GUILayout.BeginHorizontal();
// button to show/hide textfield
if (GUILayout.Button( show[i] ? "hide" : "show" ))
{
show[i] = !show[i];
}
// button to focus textfield
if (GUILayout.Button( "focus " + i ) ) {
GUI.FocusControl( "" + i );
}
GUILayout.EndHorizontal();
}
GUILayout.BeginHorizontal();
// So... here we get incorrect result
// I think textfield shifted on place of another one. Visual selection are correect and any operatin with also
// But the name of focused control is wrong!
GUILayout.Label( "Name of focused control: " + GUI.GetNameOfFocusedControl() );
GUILayout.EndHorizontal();
}
}