I’m attempting to work around the “deselect control when adding new GUI elements”… behavior.
I go the reselection to work fine, but when I reselect the TextField with GUI.FosucControl, the cursor position is lost, and the text in the TextField is all selected.
Is there anyway to automatically set the cursor position and current selection data, so that when I do refresh, the user is unaware?
(Alternatively, a way to keep focus on the control while creating new GUI elements would work fine as well.)
private int lastCursorPos = 0;
private int lastSelectCursorPos = 0;
private bool needsRefocus = false;
void OnGUI()
{
GUI.SetNextControlName("myTextField");
Text = GUI.TextField(textRect, Text, maxLength);
TextEditor te = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
if (needsRefocus)
{
needsRefocus = false;
GUI.FocusControl("myTextField");
//needs to be redefined after setting the FocusControl
te = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
if (te != null)
{
//these two lines prevent a "select all" effect on the textfield which seems to be the default GUI.FocusControl behavior
te.pos = lastCursorPos; //set cursor position
te.selectPos = lastSelectCursorPos; //set selection cursor position
}
}
if (te != null)
{
lastCursorPos = te.pos; //get cursor position on each update
lastSelectCursorPos = te.selectPos; //get selection cursor position on each update
}
}
Old question, I know, but I found that the answer above did not work exactly, not for Unity5.3.x anyway.
My solution is to first focus on the control I want, and only then draw the control itself.
Example code:
// the following code will attempt to focus on the first control ("My Text") as long as the user hasn't focused on the second control
// all this inside OnGUI ()
TextEditor te;
GUI.SetNextControlName ( "My Text" );
// this first part checks if nothing is focused, and focuses on the My Text control
if ( GUI.GetNameOfFocusedControl () != "My Text" && GUI.GetNameOfFocusedControl () != "Other Text" )
{
GUI.FocusControl ( "My Text" );
}
myText = GUILayout.TextField ( myText );
GUI.SetNextControlName ( "Other Text" );
myOtherText = GUILayout.TextField ( myOtherText );
// this one is for when tabbing between the two controls
if ( GUI.GetNameOfFocusedControl () == "My Text" )
{
te = GUIUtility.GetStateObject ( TextEditor, GUIUtility.keyboardControl );
if ( GUI.GetNameOfFocusedControl () != lastFocusedControl )
{
te.cursorIndex = lastTextPosition;
te.selectIndex = lastTextSelectPosition;
} else
{
lastTextPosition = te.cursorIndex;
lastTextSelectPosition = te.selectIndex;
}
}
lastFocusedControl = GUI.GetNameOfFocusedControl ();
GUI.FocusControl(“MyTextField”);
editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
editor.OnFocus();
editor.cursorIndex = 0 ; //CursorStartPosition;
editor.selectIndex = 56; //cursor selected end position… it will selecting the text from 0 to 56 (cursorindex)