Search Unity

Feedback 2 basic features that InputField really needs to get

Discussion in 'UGUI & TextMesh Pro' started by Sinister-Design, Apr 2, 2019.

  1. Sinister-Design

    Sinister-Design

    Joined:
    Sep 19, 2015
    Posts:
    66
    Okay. I've been working with Unity UI's InputField class for some time now, and I'm frustrated enough by its limitations that I felt the need to start a thread. I have two major complaints about it, along with suggestions for how to fix them!

    • PROBLEM 1: The ActivateInputField() method auto-selects all text in the activated InputField. This is hugely inappropriate for large text fields--it leads to frustrating situations where users accidentally erase loads of text when they start typing, having expected the cursor to appear at the end of the field. People have been complaining about this for years, and nothing has been done.
    • SOLUTION 1: As of right now, the only solution I've seen involves scripting a custom extension to Input Field, a tricky and deeply-user-unfriendly means of achieving a basic feature that really ought to be available out of the box. To fix the problem, the Input Field (Script) component should receive a radio button in the editor that, when toggled on, sets a boolean in the script that calls MoveToEnd() in LateUpdate upon activation. This will give developers the choice of whether to use the default "select all" behavior or not.

    • PROBLEM 2: Input fields do not support custom character validation. As of right now, an input field set to custom content has only six preset options for character validation: None, Integer, Decimal, Alphanumeric, Name, and Email Address. This would merely be disappointing on its own--but in combination with Problem 1, it leads to nightmare situations like this:
    • developer has a collection of multi-line InputFields that he wants the user to be able to navigate through using the Tab key. (He does not want Tab to navigate through other UI elements--just the input fields.)
    • developer puts all the input fields into a list, then implements a listener in Update()-- Input.GetKeyDown( KeyCode.Tab )--and uses it to call ActivateInputField() on the next InputField in the list. Because of Problem 1, every time this happens, the input field that gets activated will auto-select all of its text.
    • using this tab navigation feature to quickly jump between several multi-line InputFields will then result in the contents of each field being erased and replaced with a tab. Because of Problem 2, there is no way for the developer to specifically prohibit typing in tab characters in this way without also excluding other white space characters or introducing unwanted behaviors (like auto-capitalization).
    • SOLUTION 2: To fix the problem, the Input Field (Script) component should receive a Custom character validation type. When selected, Custom character validation should add a field to the editor component where excluded characters can be typed in--perhaps utilizing a variant on RegEx syntax. (If left blank, the InputField would simply behave as though it had a character validation type of None.)

    I'd be very interested to hear from a Unity developer about the feasibility of implementing one or both of these features.
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    The TextMesh Pro Input Field (TMP_InputField) has provide the functionality described in Problem 1 for a while now. As seen below the TMP Input Field includes the ability to control the selection of text when activating / focusing the input field.

    upload_2019-4-2_15-29-47.png

    When the input field is activated with the OnFocus - Select All disabled, the cursor is inserted at the start of the text but this can easily be changed as follows:

    Code (csharp):
    1.  
    2. InputFieldComponent.MoveTextEnd(false);
    3. InputFieldComponent.ActivateInputField();
    4.  
    Basically, when activating the Input Field, you can actually place the cursor or setup the selection as you desire.

    In terms of Problem 2, there is also a Custom Character validation mode (Regex) available in the TMP Input Field as seen below.

    upload_2019-4-2_15-35-10.png

    There is also a Custom Validator but that needs a bit more love on my part. There is also the ability to use the InputFieldComponent.onValidateInput = OnValidateInput; event to implement your own validation.
     
    Sinister-Design likes this.
  3. Sinister-Design

    Sinister-Design

    Joined:
    Sep 19, 2015
    Posts:
    66
    Thanks Stephan, I really appreciate the info!

    To be clear: TextMesh Pro works as a UI element in Canvases? (I guess I'd always assumed that TextMesh Pro was just a replacement for, y'know, the regular TextMesh.)
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    There are two TextMesh Pro components. One that is a replacement for the old TextMesh located in "Create - 3D Object - Text - TextMeshPro" and the 2nd found in "Create - UI - Text - TextMeshPro".

    The <TextMeshProUGUI> component is a replacement for the UI.Text and the other TMP components seen below are also replacements for the other UI components.

    upload_2019-4-3_12-10-43.png
     
  5. Sinister-Design

    Sinister-Design

    Joined:
    Sep 19, 2015
    Posts:
    66
    Got it--thank you for the detailed answers!