- Home /
TextField not clear after focusing it through a Key
Greetings unity community,
I have a chat field that is represented by a GUI.Window and a TextField at the bottom of that window.
function OnGUI() {
window = GUI.Window(1, window, GlobalChatWindow, "Chat");
}
function GlobalChatWindow(id : int) {
/*here be actual chat window logic etc, not really important as it works*/
//sets the control name
GUI.SetNextControlName("chatField");
//creates an actual field
inputField = GUILayout.TextField(inputField);
//getting focus when pressing the 'T' button
if (Input.GetKeyDown(KeyCode.T)) {
GUI.FocusControl ("chatField");
}
}
The problem hereby is, that the inputField takes on the 't' string and I can't seem to delete that t as the logic records it when I'm out of the if logic already.
Say the TextField is unfocused and I'm running around ingame. Then I press 'T'. The field gets focused but with the content 't' which I have to delete manually afterwards.
How do I get rid of this? inputField = ""; either doesn't work at all (when inside the if logic) or too good and blocks all chatting when outside the if logic.
Greetings, Cirrus
I solved it the following way
var focused : boolean = false;
function OnGUI() {
window = GUI.Window(1, window, GlobalChatWindow, "Chat");
}
function GlobalChatWindow(id : int) {
/*here be actual chat window logic etc, not really important as it works*/
//getting focus when pressing the 'T' button
if (Input.Get$$anonymous$$eyDown(UnityEngine.$$anonymous$$eyCode.T)) {
GUI.FocusControl ("chatField");
inputField = "";
focused = true;
}
//checks if it was focused during this run
//if yes, check the length
// and check whether the 't' is there,
//delete the 't' and set focused to false,
//all other run throughs will be able to accept 't'
// as a first char in the string
if (inputField.Length > 0 && focused) {
if (inputField.IndexOf('t') == 0 && inputField.Length < 2) {
inputField = "";
focused = false;
}
}
//sets the control name
GUI.SetNextControlName("chatField");
//creates an actual field
inputField = GUILayout.TextField(inputField);
}
It's not beautiful but it works. $$anonymous$$aybe there is still a better way
Answer by robertbu · Aug 17, 2013 at 11:57 PM
Here is an alternate solution. I cannot say it is better:
function OnGUI() {
var e = Event.current;
window = GUI.Window(1, window, GlobalChatWindow, "Chat");
//getting focus when pressing the 'T' button
if (e.type == EventType.KeyUp && e.keyCode == KeyCode.T && GUI.GetNameOfFocusedControl() != "chatField") {
inputField = "";
GUI.FocusControl ("chatField");
focused = true;
}
}
function GlobalChatWindow(id : int) {
/*here be actual chat window logic etc, not really important as it works*/
GUI.SetNextControlName("chatField");
//creates an actual field
var st = GUILayout.TextField(inputField);
// Don't save the string if we don't have focus
if (GUI.GetNameOfFocusedControl() == "chatField")
inputField = st;
}
Here is a bit cleaner solution. I hesitate to include it since I don't know if there are any implications to rewriting the character in the event. $$anonymous$$aybe someone with inner knowledge of Unity can comment:
function OnGUI() {
var e = Event.current;
//getting focus when pressing the 'T' button
if (e.type == EventType.$$anonymous$$eyUp && e.keyCode == $$anonymous$$eyCode.T && GUI.GetNameOfFocusedControl() != "chatField") {
inputField = "";
GUI.FocusControl ("chatField");
focused = true;
e.character = 0;
}
window = GUI.Window(1, window, GlobalChatWindow, "Chat");
}
function GlobalChatWindow(id : int) {
/*here be actual chat window logic etc, not really important as it works*/
GUI.SetNextControlName("chatField");
//creates an actual field
inputField = GUILayout.TextField(inputField);
}
Your answer
Follow this Question
Related Questions
Inputing an Equation 0 Answers
Input versus Event 1 Answer
How do you get Unity to return a user input? 1 Answer
Setting Scroll View Width GUILayout 1 Answer
Joystick ellipse movement (2d mobile asset modification) 1 Answer