- Home /
clear textfield from default string
Hi there, I have a GUI.TextField with a default value "Name". When the player activates the text field when it still contains the default value, I want it to be cleared.
This is what I tried but the problem is that, when it contains the default value, you have to click twice in order to enter text.
//draw the name input field for the player
if(Players[i].name=="Name")
{
//if the name is still the default name, clear it
if(GUI.Button(Rect (topLeft.x+40, topLeft.y+logo.height+i*playerItemHeight, 220, playerItemHeight),"",invisible))
{
Players[i].name="";
}
}
Players[i].name=GUI.TextField(Rect (topLeft.x+40, topLeft.y+logo.height+i*playerItemHeight, 220, playerItemHeight), Players[i].name,11,listName);
Hi , i am a beginner. Please tell me If i have two textfield then how can i manage.
Answer by ronsenval · Feb 12, 2012 at 01:19 PM
Another variant to do this is to use GUI.SetNextControlName and GUI.GetNameOfFocusedControl:
public class Test : MonoBehaviour
{
private string _default = "name";
private string _player_name = "";
private void OnGUI()
{
GUI.SetNextControlName ("player_name");
_player_name = GUI.TextField(new Rect(0, 0, 200, 30), _player_name);
if (UnityEngine.Event.current.type == EventType.Repaint)
{
if (GUI.GetNameOfFocusedControl () == "player_name")
{
if (_player_name == _default) _player_name = "";
}
else
{
if (_player_name == "") _player_name = _default;
}
}
}
}
That's what i was talking about :D thanks a lot! Didn't know about this focused control stuff.
Answer by ronsenval · Feb 10, 2012 at 07:15 AM
You can do this using tooltip:
public class Test : MonoBehaviour
{
private string _default = "name";
private string _player_name = "";
private string _tooltip = "player_name_field";
private void OnGUI()
{
GUI.BeginGroup(new Rect(0, 0, 200, 30), new GUIContent("", _tooltip));
_player_name = GUI.TextField(new Rect(0, 0, 200, 30), _player_name);
GUI.EndGroup();
if (UnityEngine.Event.current.type == EventType.Repaint)
{
if (GUI.tooltip == _tooltip)
{
if (_player_name == _default) _player_name = "";
}
else
{
if (_player_name == "") _player_name = _default;
}
}
}
}
The idea is when is mouse over textfield and it has default value then clear it. And when mouse is out and textfield is cleared, then set it to default value.
Hi, great Idea! Unfortunately, this GUI.BeginGroup/endGroup makes my text box invisible :/ how does that work exactly? Is there a way to do this without a GUI group?
EDIT: ok, this was because the GUIgroup overrode the GUIstyle of the name input box item. But still, it doesn't work.
if I do
GUI.BeginGroup(Rect (topLeft.x+40, topLeft.y+logo.height+i*playerItemHeight, 220, playerItemHeight), GUIContent("", "namefield"),listName);
Players[i].name=GUI.TextField(Rect (topLeft.x+40, topLeft.y+logo.height+i*playerItemHeight, 220, playerItemHeight), Players[i].name,11,listName);
GUI.EndGroup();
the box just doesn't do a thing and I can't click it. :/
Another variant (the main idea is the same, but we use mouse position to detect when mouse is over textfield):
public class Test : $$anonymous$$onoBehaviour
{
private string _default = "name";
private string _player_name = "";
private Rect _textfield_position = new Rect(0, 0, 200, 30);
private void OnGUI()
{
_player_name = GUI.TextField(_textfield_position, _player_name);
if (UnityEngine.Event.current.type == EventType.Repaint)
{
if (_textfield_position.Contains(new Vector2(Input.mousePosition.x, Screen.height - Input.mousePosition.y)))
{
if (_player_name == _default) _player_name = "";
}
else
{
if (_player_name == "") _player_name = _default;
}
}
}
}
How is it work, if you remove style from GUIGroup (in variant with tooltip)? I thought it would be invisible, but have tooltip.
thanks. works bow but when people enter text, they usually move the cursor away from the text field so the placeholder is back again :/ any other idea to do this with a clic ins$$anonymous$$d of hover?
Answer by ronsenval · Feb 10, 2012 at 07:15 AM
Maybe you can do so: Add tooltip (for example "name_text_field") to text field. Then check if mouse is over tooltip (GUI.tooltip == "name_text_field") and value of text field is still default (Players[i].name == "name" ) then clear text field. If mouse is out of text field and text field is empty, then set text field value to default.
Your answer
Follow this Question
Related Questions
How do you get a caret to show in GUILayout.TextField ??? 0 Answers
text box and button won't display 2 Answers
Making Text Boxes With Letters Appearing One At A Time 2 Answers
In Game Notepad 2 Answers
3D Textbox 0 Answers