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 /
  • Help Room /
avatar image
4
Question by rjth · Jan 05, 2017 at 11:59 AM · uiinputfield

How to disable SelectAll() text of InputField OnFocus()?

The UI InputField when it gets focus highlights all text inside. I would like to move the caret to the end of the text so the user can keep writing the text within. Currently I have a hack solution, which works, however there it is still a short moment when the text is highlighted. Here is my hack:

 using UnityEngine;
 using UnityEngine.EventSystems;
 using UnityEngine.UI;
 
 public class TextFieldBehaviour : MonoBehaviour, ISelectHandler
 {
     private InputField inputField;
     private bool isCaretPositionReset = false;
 
     void Start()
     {
         inputField = gameObject.GetComponent<InputField>();
     }
 
     public void OnSelect (BaseEventData eventData) 
     {
         isCaretPositionReset = false;
     }
 
     void Update()
     {
         if(inputField.isFocused == true && isCaretPositionReset == false)
         {
             inputField.caretPosition = inputField.text.Length;
             isCaretPositionReset = true;
         }
     }
 }

I was also checking out the source code of InputField. But I have trouble trouble creating a custom one without the SelectAll() function. I am getting a bunch of errors due to the protection level of the UnityEngine.UI.SetPropertyUtility.

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
5
Best Answer

Answer by rjth · Jan 05, 2017 at 03:16 PM

Okay so I've figured it out. I needed to inherit from the original InputField and extend it with the necessary functionality. Here is the working script:

 using UnityEngine;
 using UnityEngine.UI;
 using UnityEngine.EventSystems;
 
 public class CustomInputField : InputField
 {
     new public bool Focused = false;
     new public bool Deactivated = false;
 
     new public void ActivateInputField()
     {
         Focused = true;
         base.ActivateInputField();
     }
 
     public override void OnDeselect(BaseEventData eventData)
     {
         Deactivated = true;
         DeactivateInputField();
         base.OnDeselect(eventData);
     }
 
     public override void OnPointerClick(PointerEventData eventData)
     {
         if(Deactivated)
         {
             MoveTextEnd(true);
             Deactivated = false;
         }
         base.OnPointerClick(eventData);
     }
 
     protected override void LateUpdate()
     {
         base.LateUpdate();
         if(Focused)
         {
             MoveTextEnd(true);
             Focused = false;
         }
     }
 }
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 SanyaBane · Feb 08, 2020 at 02:18 PM 0
Share

I thought i doesn't work, but it was just because i was absent$$anonymous$$dedly use "ActivateInputField()" from "InputFIeld"-reference, ins$$anonymous$$d of "CustomInputField"-reference, geez...

As on stackoverflow-theme mentioned, its still have problems with short (1 frame probably?) highlighting of text when you use $$anonymous$$ouse to click on this InputField, but it works perfectly in case you need to set focus just programmatically using ActivateInputField() which is exactly what i need. Thanks!

P.S. I will rename "ActivateInputField()" to "ActivateInputFieldWithoutSelectAll()" and remove "new" on my side to avoid such stupid situations as I described above :)

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

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

Limit line length on input field 0 Answers

Check if InputField is empty 1 Answer

Input field keyboard not showing up after building it on android 0 Answers

Checking if player is typing in an InputField 2 Answers

Destroy something onSubmit of InputField. 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