- Home /
Detect Text in GUI; Print
Hello, I have a bit of a compound question. With the below script, is it possible to detect what the user has input only AFTER he has pressed enter, then detect that value and print IN THE SAME GUI TEXT? Thanks everyone who can help.
My script:
var stringToEdit : String = "Hello World";
var TextStyle = new GUIStyle();
function OnGUI () {
stringToEdit = GUI.TextField(Rect(0,0,Screen.width,Screen.height), stringToEdit, 1000000000, TextStyle);
}
I did actually look into this method (below) but it detected it before I pressed enter.
if (stringToEdit == "TEST" && Input.KeyCode.Return) {
print("Recieved");
}
Answer by Khada · Mar 12, 2013 at 03:55 PM
var stringToEdit : String = "Hello World";
var TextStyle = new GUIStyle();
var typing : boolean = false;
function OnGUI ()
{
if(Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return && !typing)
typing = true;
else if(Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return && typing)
typing = false;
var temp : String = stringToEdit;
stringToEdit = GUI.TextField(Rect(0,0,Screen.width,Screen.height), stringToEdit, 1000000000, TextStyle);
if(!typing)
stringToEdit = temp;
}
Good one, I understand and by all rights it should work but... the GUI is gone...?
No no, you misunderstand. I mean I only want it to DETECT what I'm typing WHEN I PRESS ENTER. Then out put something depending on the input.
Can you still help?
I've edited my answer so that the text field remains visible.
Okay, unity wants a semi-colon at line 13. Sorry for drawing this out...
Your answer
Follow this Question
Related Questions
Limit on GUI Components? 0 Answers
format text block in GUI.Box 1 Answer
Setting Scroll View Width GUILayout 1 Answer
GUI text blinking opacity 1 Answer
How do I set a GUI button's text using a string from another script? 0 Answers