- Home /
Disabling/Hiding a Text Field with Enter Key
Here's what I have so far...
var naming1 : boolean = false;
var slot1_namer : GUI;
var slot1_name : String;
function Update (){
if(Input.GetKeyDown(Keycode.Return))
{
if(naming1 == true)
{
naming1 = false;
}
else if(naming1 == false)
{
naming1 = true;
}
}
}
function OnGUI (){
if(naming1 == true)
{
slot1_namer.enabled = true;
slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
if(Input.GetKeyDown(Keycode.Return))
{
slot1_namer.enabled = false;
slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
}
}
else if(naming1 == false)
{
slot1_namer.enabled = false;
slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
}
}
redundant in the end, I know, but I was REALLY trying to get this to work. So how can I disable (or ideally "hide") the textfield when "return" or "enter" is pressed?
EDIT: New development, every time I leave then go back to the menu with the TextFields, the ability for "enter" to enable and disable them is reset. Meaning that as long as none of the fields are edited, the code works properly and they fields enable and disable. yet if any of them are edited, this stops working so I have to back out then go back in.
Answer by Tomer-Barkan · May 20, 2013 at 06:13 AM
How about:
public class SomeClass : MonoBehaviour {
private bool textFieldEnabled = true;
private string textFieldText = "";
void update() {
if(Input.GetKeyDown(Keycode.Return)) {
textFieldEnabled = !textFieldEnabled;
}
}
void OnGUI() {
if (textFieldEnabled) {
textFieldText = GUI.TextField(new Rect(0, 0, 100, 25), textFieldText);
}
}
}
didn't work. I mean, it initially hides it but afterwards I still run into the same problem of "return" not disabling it.
my bad, you shouldn't handle key inputs in OnGUI(). See http://answers.unity3d.com/questions/243995/getkeydown-problem.html for more info. Updated the code so that it now works (tested myself)
Still not working on my end. I press enter, the TextField stays. But like before it hides it initially then shows it after clicking on the button that shows it. This is a lot more complicated than the posted code, but I can't post it all since it's over 300 lines long. Suffice to say, there is a button that, when clicked sets na$$anonymous$$g1 to true. then when I press enter, it SHOULD set it to false, thereby disabling or hiding the textfield. Yet this is not the case.
$$anonymous$$y code works perfectly, I tested it. So if I'm to help you I'll need more info on whats different between your code and $$anonymous$$e.
well... is there a place I can upload the full code? it's well over the 3000 character limit.
I'm sure nothing is wrong with your code, $$anonymous$$e is just so tangled anything could be going wrong
Answer by LukaKotar · May 20, 2013 at 06:33 AM
Try removing all lines from the 24th to 28th line. Basically in the Update() you are changing the boolean to the opposite value, so if you set it to 'false' in OnGUI(), the Update() will turn it 'true'.
I tried when it was just in the update function and when it wasn't, both times I'm still not getting exactly the desired result. It seems as though using GUI buttons will disable/hide the buttons (this is only a portion of a much larger script utilizing many buttons) because when I click the "back" button and access the menu again (through a different button) then they're all disabled and uneditable.
I should be more specific...
They're all part of a menu that has "resume, save, load, and exit" on it. When going to "save" the text fields aren't having their desired result. But when I press "back" from the saving/loading GUI group then go back to the saving/loading GUI group via the "loading" button (the only difference is a variable called "loading" that is a boolean that is set to true while "saving" is set to false, it also disables na$$anonymous$$g1 but seems to actually have an effect) then it works.
Actually there was a mistake on my side here - I thought you were setting 'na$$anonymous$$g1' to false here... $$anonymous$$aybe try making the Update() function handle all the input. That way you can have more control over when the events happen. Try doing something like this:
var na$$anonymous$$g1 : boolean = false;
var slot1_namer : GUI;
var slot1_name : String;
function Update (){
if(Input.Get$$anonymous$$eyDown($$anonymous$$eycode.Return))
{
if(na$$anonymous$$g1 == true)
{
slot1_namer.enabled = false;
slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
na$$anonymous$$g1 = false;
}
else if(na$$anonymous$$g1 == false)
{
na$$anonymous$$g1 = true;
}
}
}
function OnGUI (){
if(na$$anonymous$$g1 == true)
{
slot1_namer.enabled = true;
slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
}
else if(na$$anonymous$$g1 == false)
{
slot1_namer.enabled = false;
slot1_name = slot1_namer.TextField(new Rect(0,0,100,25),slot1_name,25);
}
}
Answer by unimechanic · Jul 03, 2013 at 04:08 PM
This example might also help:
http://forum.unity3d.com/threads/188202-Simple-dynamic-list-editor
Answer by Franco-Marini · Aug 19, 2021 at 01:31 AM
Use this code. It will solve all focus problem and key detection. @jeffreywarf
public class MyConsole : MonoBehaviour
{
// Start is called before the first frame update
public bool showConsole = false;
public string guiText = "";
void Start()
{
}
void Update()
{
if (Input.GetKeyUp(KeyCode.F5)) showConsole = !showConsole;
}
private void OnGUI()
{
if (showConsole) {
GUI.SetNextControlName("MyTextField");
GUI.FocusControl("MyTextField");
guiText = GUI.TextField(new Rect(10, 10, 1000, 20), guiText, 200);
}
if (Event.current.keyCode == KeyCode.Return && showConsole)
{
//DO SOMETHING WITH THE TEXT AND THEN CLEAR TEXT
//ADD SOME CODE
guiText = "";
}
}
}