Hi,
I was in the process of creating an editor extension for my game when I came across a weird problem with SetNextControlName function. I have created a little showcase for everyone to check. Here the code:
public override void OnInspectorGUI ()
{
Event e = Event.current;
if (e.isMouse e.type == EventType.MouseUp e.button == 0)
{
Debug.Log(GUI.GetNameOfFocusedControl());
}
GUI.SetNextControlName("t1");
t1=EditorGUILayout.TextField(t1);
if (t1 != "a")
{
GUI.SetNextControlName("t2");
EditorGUILayout.TextField("");
}
GUI.SetNextControlName("t3");
EditorGUILayout.TextField("");
if (GUI.changed)
EditorUtility.SetDirty (target);
}
What this script does is pretty simple. It creates 2 or 3 controls depending on the text inserted in t1 and when a control is clicked it shows its asigned name.
Each control is assigned a name. The problem is that when t1 does not contain “a” the three controls are shown and they report their names correctly but when a is inserted in t1 the last control when clicked does not show “t3” but “t2”.
What I expect as I have said, is when 3 items are shown that they had names t1,t2,t3 and when just 2 are shown they had names t1 and t3.
Could anyone explain this weird behaviour? Could it be a bug?
Thanks in advance.
I see see a clear problems with your code. The UnityGUI is an event driven system so your OnInspectorGUI method gets called multiple times per frame. You’re only allowed to change the number of controls and their names during the Event.current.type == EventType.Layout pass. This means that what you see is probably a mix between the old layout names and the new layout controls. I expect that once you abide this rule everything should work as intended.
Thanks a lot or the insights, but I think I need a bit more help with this:
I have reweritten the previous to:
public override void OnInspectorGUI()
{
Event e = Event.current;
if (e.isMouse e.type == EventType.MouseUp e.button == 0)
{
Debug.Log(GUI.GetNameOfFocusedControl());
}
//If I use just EventType.Layout here no control is shown
if (e.type == EventType.Layout || e.type==EventType.Repaint)
{
GUI.SetNextControlName("t1");
t1 = EditorGUILayout.TextField(t1);
if (t1 != "a")
{
GUI.SetNextControlName("t2");
EditorGUILayout.TextField("");
}
GUI.SetNextControlName("t3");
EditorGUILayout.TextField("");
}
if (GUI.changed)
EditorUtility.SetDirty(target);
}
Now when I click a control it allways print a blank name string :/. I’m getting a bit crazy with this. I need to use something like this because I’m building a grid of data. Every row can be deleted in place and all controls in a row are bound by “name”. I mean for example controls on row 1 have name “R1”. This way when I detect that a row has left focus(because we are moving to another control with different name, for example “R2” I do all the validation code and save the row data.
If you could provide a simple example based in my code that works I would really apreciate it. From your words I suspect it will be really laboriuos to do this kind of things.
Thanks in advance.
Hi,
Sorry for the bump, but I need this to be resolved. I have read a bunch of documentation, seen others plugins source code, but no solution for this. I would really apreciate if anyone could help here.
Thanks in advance.