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
2
Question by sharsnik · Jan 27, 2012 at 05:05 PM · guitextfieldfocus

Using GUI.FocusControl on TextField selects all text

I'm attempting to work around the "deselect control when adding new GUI elements".... behavior.

I go the reselection to work fine, but when I reselect the TextField with GUI.FosucControl, the cursor position is lost, and the text in the TextField is all selected.

Is there anyway to automatically set the cursor position and current selection data, so that when I do refresh, the user is unaware?

(Alternatively, a way to keep focus on the control while creating new GUI elements would work fine as well.)

Comment
Add comment · Show 4
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 Berenger · Mar 15, 2012 at 11:50 PM 0
Share

If someone know the answer, I'd appreciate it as well.

avatar image Morre · Jul 31, 2012 at 07:17 AM 0
Share

I've also been trying to find a solution to this. Any assistance would be great! :)

avatar image Morre · Jul 31, 2012 at 08:39 AM 0
Share

I've also been trying to find a solution to this. Any assistance would be great! :)

avatar image sharsnik · Aug 04, 2012 at 09:40 AM 0
Share

Well, I can't give you an answer... ultimately I had to rewrite the TextField behavior from scratch.

4 Replies

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

Answer by fabr.IQ · Feb 25, 2013 at 07:22 PM

Here's what I did:

 private int lastCursorPos = 0;
 private int lastSelectCursorPos = 0;

 private bool needsRefocus = false;
 
 void OnGUI()
 {
      GUI.SetNextControlName("myTextField");
      Text = GUI.TextField(textRect, Text, maxLength);
             
      TextEditor te = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
 
      if (needsRefocus)
      {
           needsRefocus = false;

           GUI.FocusControl("myTextField");

           //needs to be redefined after setting the FocusControl        
           te = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);  
                  
           if (te != null)
           {
                //these two lines prevent a "select all" effect on the textfield which seems to be the default GUI.FocusControl behavior
                te.pos = lastCursorPos;  //set cursor position
                te.selectPos = lastSelectCursorPos;  //set selection cursor position
           }
      }

      if (te != null)
      {
           lastCursorPos = te.pos;  //get cursor position on each update
           lastSelectCursorPos = te.selectPos;  //get selection cursor position on each update
      }
 }

 
Comment
Add comment · Show 2 · 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 Helical · Oct 09, 2014 at 01:44 PM 1
Share

Your solution almost worked for me. I was not aware of the TextEditor or GUIUtility things.

basically i had to replace your 2 lines that set the cursur to the end by these two lines

         te.SelectNone();
         te.$$anonymous$$oveTextEnd();

then it worked for me

avatar image jesusluvsyooh Helical · Apr 19, 2016 at 07:03 PM 0
Share

Thanks, a combination of everyones answers and replies helped me fix my problem :)

avatar image
1

Answer by TheDemiurge · Mar 02, 2016 at 02:11 AM

Old question, I know, but I found that the answer above did not work exactly, not for Unity5.3.x anyway. My solution is to first focus on the control I want, and only then draw the control itself.

Example code:

 // the following code will attempt to focus on the first control ("My Text") as long as the user hasn't focused on the second control
 // all this inside OnGUI ()
 TextEditor te;
 GUI.SetNextControlName ( "My Text" );

 // this first part checks if nothing is focused, and focuses on the My Text control
 if ( GUI.GetNameOfFocusedControl () != "My Text" && GUI.GetNameOfFocusedControl () != "Other Text" )
 {
     GUI.FocusControl  ( "My Text" );
 }
 myText = GUILayout.TextField ( myText );
 GUI.SetNextControlName ( "Other Text" );
 myOtherText = GUILayout.TextField ( myOtherText );

 // this one is for when tabbing between the two controls
 if ( GUI.GetNameOfFocusedControl () == "My Text" )
 {
     te = GUIUtility.GetStateObject ( TextEditor, GUIUtility.keyboardControl );
     if ( GUI.GetNameOfFocusedControl () != lastFocusedControl )
     {
         te.cursorIndex = lastTextPosition;
         te.selectIndex = lastTextSelectPosition;
     } else
     {
         lastTextPosition = te.cursorIndex;
         lastTextSelectPosition = te.selectIndex;
     }
 }
 lastFocusedControl = GUI.GetNameOfFocusedControl ();
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
avatar image
0

Answer by banniandoko · Nov 17, 2016 at 12:09 PM

 GUI.FocusControl("MyTextField");                    
                     editor = (TextEditor)GUIUtility.GetStateObject(typeof(TextEditor), GUIUtility.keyboardControl);
                     editor.OnFocus();
                     editor.cursorIndex = 0 ; //CursorStartPosition;
                     editor.selectIndex = 56; //cursor selected end position.. it will selecting the text from 0 to 56 (cursorindex)
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
avatar image
0

Answer by THplusplusx · May 24, 2017 at 07:38 AM

This snippet should prevent auto-select-all for any text based input field (tested with TextArea and IntField).

 private T WithoutSelectAll<T>(Func<T> guiCall)
 {
     bool preventSelection = (Event.current.type == EventType.MouseDown);
     Color oldCursorColor = GUI.skin.settings.cursorColor;
 
     if (preventSelection)
         GUI.skin.settings.cursorColor = new Color(0, 0, 0, 0);
 
     T value = guiCall();
 
     if (preventSelection)
         GUI.skin.settings.cursorColor = oldCursorColor;
 
     return value;
 }

Use like:

 int foo;
 string bar;
 
 foo = WithoutSelectAll(() => GUI.IntField("foo", foo));
 bar = WithoutSelectAll(() => EditorGUILayout.TextArea(bar));
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

12 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

Related Questions

TextField not getting focus on iOS 1 Answer

TextField, Event.current, Input.GetKey, and GUI.FocusControl locking 1 Answer

GUI.FocusControl & GUI.SetNextControlName doesn't work 2 Answers

Text fields where text will scale along with resolution. 1 Answer

GUI custom textfield cursor rendering 2 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