GUITextField

= GUITextField =

Description
The Unity provided GUI.TextField and GUILayout.TextField are of very limited use for editing strings that need to be sent over a network (e.g. player names). The new text is returned after every call to the TextField function, and even testing whether it's changed or not before sending it means that a new string will be sent over the network with each character change.

In addition, where an authoritative server is used to check the name and confirm that the name has been changed, this effectively means the client is typing "over the network" with all the latency issues that causes (and hence problems with a confirmed name arriving after new characters have been typed and scrambling the intended characters).

The following script therefore keeps track of when the text field is being edited, and returns true from the display call only when new text is commited (i.e. the text field is changed, then unfocused by a mouse click elsewhere or by pressing return / enter). It also returns false, and restores the unedited text if the user presses escape.

Unfortunately, provision for focusing of controls is a bit sketchy in Unity (it's possible to focus a specific control, but not to unfocus one), so a "fake" control is provided, and GUITextField.FocusHack must be called before TextField.Display in an OnGUI function. In addition, a text field must be identified with a unique control name.