No GUI.UnfocusControl... what's the workaround?

There is the focus control:

… but there is no GUI.UnfocusControl.

But I find that in Editor Windows, the focus can “get stuck”. If you select a field:

and then change what you are looking at, in this case looking at a new item:

because the field was focused, it doesn’t update the data correctly (it still days 17 in the current item field):

so when the field is manually refocussed by clicking into another field (in this case the search field):

… the correct data is displayed.

This could be solved with a “GUI.UnfocusControl”, there is a GUI.UnfocusWindow:

But no “GUI.UnfocusControl”.

I’ve tried to work-around it by setting a “GUILayout.Label” as a named control and swapping focus to that control using GUI.FocusControl, but it doesn’t work. With this code:

	GUI.SetNextControlName ("shiftFocus");
	GUILayout.Label ("Inventory Item Editor", EditorStyles.boldLabel);

	viewIndex = EditorGUILayout.IntField ("Current Item", viewIndex, GUILayout.ExpandWidth(false));

… this seems to skip the GUILayout.Label as a control and sets the EditorGUILayout.IntField as the field to shift focus to. Which I don’t want. I was hoping for an invisible control to shift the focus to, but I can’t find a way to shift focus to anything other than a usable field… not a great work around.

Anyone else find a solution or have any suggestions?

At the bottom of your GUI, just put GUI.SetNextControlName(“”); Then you can just call GUI.FocusControl (“”); to unfocus items.

It works for me so far. Let me know if you have any problems with that.

1 Like

Oooh. Clever.

That works, it does!

Thanks for the tip.

Hmm… How about a work around for this one:

If I have focus on a field:

… and want to create or move to a new record:

… but also want to set the focus:

Currently I’ve tried first dropping focus - which works on its own following the suggestion above…

Then I’ve tried a number of attempts to refocus on the field, with code like this:

	void OnGUI () {
		//	snip - Irrelevant code in OnGUI()...
		if (GUILayout.Button("Add Item", GUILayout.ExpandWidth(false))) {
			GUI.FocusControl ("clearFocus");
			AddItem();
		}
	}

	void AddItem () {
		//	snip - Irrelevant code to add an item
		newItem = true;
	}

And later in OnGUI():

	if (newItem) {
		newItem = false;
		GUI.FocusControl ("Item Name");
	}

But no matter where I place this in OnGUI, it grabs the previous information and retains it:

This should say “New Item”.

Bah and Bargle!

I was hoping by forcing it to drop focus and then refocusing the field, it would find the new piece of information…

Hrm…

A pause/wait for one frame co-routine could probably do the trick. I faked it with:

	void SetNewItemFocus () {
		GUI.FocusControl ("Search Field");
		newItemCount ++;
		if (newItemCount  > 1) {
			newItemCount = 0;
			viewIndex = itemList.Length;
			GUI.FocusControl ("Item Name");
			newItem = false;
		}
	}

I guess the editor window just needs a moment to get it’s wits together.

Cheers, this helped a lot!