• 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
Question by Cirrus_ · Aug 17, 2013 at 08:40 PM · guiinput

TextField not clear after focusing it through a Key

Greetings unity community,

I have a chat field that is represented by a GUI.Window and a TextField at the bottom of that window.

 function OnGUI() {
     window = GUI.Window(1, window, GlobalChatWindow, "Chat");
 }
 
 function GlobalChatWindow(id : int) {
    /*here be actual chat window logic etc, not really important as it works*/
 
     //sets the control name
     GUI.SetNextControlName("chatField");
 
         //creates an actual field
     inputField = GUILayout.TextField(inputField);
 
         //getting focus when pressing the 'T' button    
     if (Input.GetKeyDown(KeyCode.T)) {        
         GUI.FocusControl ("chatField");    
     }
 }

The problem hereby is, that the inputField takes on the 't' string and I can't seem to delete that t as the logic records it when I'm out of the if logic already.

Say the TextField is unfocused and I'm running around ingame. Then I press 'T'. The field gets focused but with the content 't' which I have to delete manually afterwards.

How do I get rid of this? inputField = ""; either doesn't work at all (when inside the if logic) or too good and blocks all chatting when outside the if logic.

Greetings, Cirrus

Comment
liortal

People who like this

1 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 Cirrus_ · Aug 17, 2013 at 08:43 PM 1
Share

I solved it the following way

 var focused : boolean = false;
 
 function OnGUI() {
     window = GUI.Window(1, window, GlobalChatWindow, "Chat");
 }
              
 function GlobalChatWindow(id : int) {
  /*here be actual chat window logic etc, not really important as it works*/
 
     //getting focus when pressing the 'T' button 
     if (Input.GetKeyDown(UnityEngine.KeyCode.T)) {        
         GUI.FocusControl ("chatField");    
         inputField = "";
         focused = true;
     }
     //checks if it was focused during this run
     //if yes, check the length
     // and check whether the 't' is there,
     //delete the 't' and set focused to false, 
     //all other run throughs will be able to accept 't'
     // as a first char in the string    
     if (inputField.Length > 0 && focused) {
         if (inputField.IndexOf('t') == 0 && inputField.Length < 2) {
             inputField = "";
             focused = false;
         }
     }
     //sets the control name
     GUI.SetNextControlName("chatField"); 
     //creates an actual field
     inputField = GUILayout.TextField(inputField);        
 }
     

It's not beautiful but it works. Maybe there is still a better way

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by robertbu · Aug 17, 2013 at 11:57 PM

Here is an alternate solution. I cannot say it is better:

 function OnGUI() {
      var e = Event.current;    
      
     window = GUI.Window(1, window, GlobalChatWindow, "Chat");
 
         //getting focus when pressing the 'T' button 
     if (e.type == EventType.KeyUp && e.keyCode == KeyCode.T && GUI.GetNameOfFocusedControl() != "chatField") {   
         inputField = "";
         GUI.FocusControl ("chatField"); 
         focused = true;
     }
 }
  
 function GlobalChatWindow(id : int) {
  /*here be actual chat window logic etc, not really important as it works*/
     GUI.SetNextControlName("chatField"); 
     //creates an actual field
     var st = GUILayout.TextField(inputField);
     // Don't save the string if we don't have focus
     if (GUI.GetNameOfFocusedControl() == "chatField")
         inputField = st;   
 }
Comment

People who like this

0 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 robertbu · Aug 18, 2013 at 12:14 AM 0
Share

Here is a bit cleaner solution. I hesitate to include it since I don't know if there are any implications to rewriting the character in the event. Maybe someone with inner knowledge of Unity can comment:

 function OnGUI() {
      var e = Event.current;    
      
     //getting focus when pressing the 'T' button 
     if (e.type == EventType.KeyUp && e.keyCode == KeyCode.T && GUI.GetNameOfFocusedControl() != "chatField") {   
         inputField = "";
         GUI.FocusControl ("chatField"); 
         focused = true;
         e.character = 0;
     }
     window = GUI.Window(1, window, GlobalChatWindow, "Chat");
 }
  
 function GlobalChatWindow(id : int) {
  /*here be actual chat window logic etc, not really important as it works*/
     GUI.SetNextControlName("chatField"); 
     //creates an actual field
     inputField = GUILayout.TextField(inputField); 
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 People are following this question.

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

Related Questions

Inputing an Equation 0 Answers

Input versus Event 1 Answer

How do you get Unity to return a user input? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Setting Scroll View Width GUILayout 1 Answer


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