Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
3
Question by Logopolis · Nov 25, 2015 at 01:12 AM · uitextinputfieldselectionhighlighting

How to deselect text in an InputField?

When an InputField is activated, either by clicking on it or by calling its ActivateInputField() function, all of the text in that input field is automatically selected (that is, highlighted, such that anything typed will replace what was already there, unless the mouse is first clicked somewhere in the text to deselect it and position the caret).

I would like to be able to either:

1) Not select the text when the InputField is activated, and just have the caret positioned at the end so that whatever is typed will be added to the end of the text that's already there; or:

2) Deselect the text and move the caret to the end, via script. I tried the MoveTextEnd() function, with the "shift" parameter set to true or false, and this did not result in the text being deselected.

Any help would be greatly appreciated!

Comment
Add comment
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

1 Reply

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

Answer by Logopolis · Nov 28, 2015 at 08:03 AM

I found a solution to this, with the help of these forum posts:

http://forum.unity3d.com/threads/inputfield-highlighting-movetextend.315091/ http://forum.unity3d.com/threads/how-to-wait-for-a-frame-in-c.24616/

The trick is that due to the way the UI system works, the selected UI element is only updated later in the frame, so moving the caret to the end (and deselecting the text) can only be done at the end of the frame, or in the next frame. Here's the code I used to move the caret to the end, and deselect the text, during the next frame after the input field is selected:

 {
     // Activate and select the chatInputField to allow the player to just continue typing.
     chatInputField.ActivateInputField();
     chatInputField.Select();
 
     // Start a coroutine to deselect text and move caret to end. 
     // This can't be done now, must be done in the next frame.
     StartCoroutine(MoveTextEnd_NextFrame());
 }
 
 IEnumerator MoveTextEnd_NextFrame()
 {
     yield return 0; // Skip the first frame in which this is called.
     chatInputField.MoveTextEnd(false); // Do this during the next frame.
 }

Note that another possible solution is to create a subclass of InputField, and have that subclass's LateUpdate() method call MoveToEnd() (after calling base.LateUpdate()). This way it can be done during the same frame in which the InputField is activated, but late enough in the frame that the text will be deselected.

Comment
Add comment · Show 3 · 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 nxAAA · Jun 20, 2016 at 03:12 AM 1
Share

Very nice. Your example worked for me. But with the help of your forum posts, I managed to get it done during the same frame. Here's some of the code relating to "my" project.

 public class $$anonymous$$ultiLineInputField : InputField {
 
     void LateUpdate() {
         base.LateUpdate();
 
         if (EventSystem.current != null)
         {
             if (this.gameObject.Equals(EventSystem.current.currentSelectedGameObject))
             {
                 $$anonymous$$oveTextEnd(false);
             }
         }
     }
 }

 public class SomeClass : $$anonymous$$onoBehaviour
 {
     [SerializeField] private $$anonymous$$ultiLineInputField myObject;
 
     public void Initialize()
     {
             myObject.ActivateInputField();
             myObject.Select();
     }
 }

avatar image Ziplock9000 nxAAA · Apr 17, 2019 at 02:02 PM 0
Share

Unfortunately that runs code on every frame no matter what happens, even if it's extremely $$anonymous$$imal. I'd much prefer a self exiting coroutine that happens very infrequently.

avatar image DDeathlonger · Jun 20, 2018 at 11:38 AM 1
Share

frickin Unity... XD They can create such a diverse and powerful engine, but they can't just throw this into the default code... sigh life goes on I suppose.... Although now I'll NEVER get those 30ish $$anonymous$$utes back!

SEO: Input Field - UI - Caret - $$anonymous$$ove Caret - Unhighlight - highlight

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

42 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 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 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

Selecting a part of string in input field 1 Answer

How do i check the inputted text in a Multi line Input field? 1 Answer

Making a UI.Button put text into a InputField 1 Answer

TextMeshPro InputField masking not working 0 Answers

Dynamic Input FIeld 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