Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Somian · Feb 09, 2012 at 09:31 PM · guitextbox

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);
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ArvindGames · Oct 18, 2014 at 04:29 PM 0
Share

Hi , i am a beginner. Please tell me If i have two textfield then how can i manage.

3 Replies

· Add your reply
  • Sort: 
avatar image
4
Best Answer

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;
         }
     }
 }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Somian · Feb 13, 2012 at 02:28 PM 0
Share

That's what i was talking about :D thanks a lot! Didn't know about this focused control stuff.

avatar image
-1

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Somian · Feb 10, 2012 at 01:11 PM 0
Share

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. :/

avatar image ronsenval · Feb 10, 2012 at 10:45 PM 0
Share

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;
         }
     }
 }
 }
avatar image ronsenval · Feb 11, 2012 at 12:09 AM 0
Share

How is it work, if you remove style from GUIGroup (in variant with tooltip)? I thought it would be invisible, but have tooltip.

avatar image Somian · Feb 11, 2012 at 06:13 PM 0
Share

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?

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges