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*.name=="Name")*
{*
//if the name is still the default name, clear it*
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*.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.
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.