Search Unity

TextMesh Pro Move cursor to end of text

Discussion in 'UGUI & TextMesh Pro' started by CityWizardGames, May 11, 2018.

  1. CityWizardGames

    CityWizardGames

    Joined:
    Nov 6, 2013
    Posts:
    21
    Hey there Stefan and team, question for you. We have the use case of programatically selecting a TMP_InputField component and setting the cursor at the end so somebody can start typing from the end.

    Both Select() and ActivateInputField() successfully give focus to the input field but select all of the text. Setting caretPosition after that doesn't seem to do anything. Tried both the MoveTextEnd() and MoveToEndOfLine() methods with no success either.
     
    AldeRoberge likes this.
  2. CityWizardGames

    CityWizardGames

    Joined:
    Nov 6, 2013
    Posts:
    21
    Turns out there's an option in "Control Settings" on the input field component for 'OnFocus - Select All', just had to un-check that. :D
     
  3. CityWizardGames

    CityWizardGames

    Joined:
    Nov 6, 2013
    Posts:
    21
    Well, I still haven't solved the "move cursor to end of text" issue, but I solved the "it's highlighting all text when selected" problem.
     
  4. PSchaussPlaya

    PSchaussPlaya

    Joined:
    Mar 2, 2018
    Posts:
    14
    inputField.MoveToEndOfLine(false, false);
    works for me.
    You may try to execute it with a delay. E.g. in a coroutine after a
    yield return new WaitForEndOfFrame();
    or in LateUpdate.
     
  5. OSagioma

    OSagioma

    Joined:
    May 12, 2015
    Posts:
    13
    Doesn't do nothing for me :(
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    This function should be working as intended as I tested it last night again.

    Please provide the full scripts where you are trying to use this function. See if you can produce a simple repro script for me to use for testing.
     
  7. OSagioma

    OSagioma

    Joined:
    May 12, 2015
    Posts:
    13
    I have fixed this the other day. A friend of mine provided an alternative.
     
    Stephan_B likes this.
  8. rcaettano

    rcaettano

    Joined:
    Dec 11, 2012
    Posts:
    6
    Kinda old, but LateUpdate worked for me, thanks !
     
  9. dr_vr

    dr_vr

    Joined:
    Dec 8, 2017
    Posts:
    2
    Use inputField.MoveToEndOfLine(false, true) if you wanna go to the end of all of the text.
     
  10. c-Row

    c-Row

    Joined:
    Nov 10, 2009
    Posts:
    853
    I got the opposite problem - I would like to keep all text selected but still move the cursor to the end of the input field. Select() and ActivateInputField() work as intended in that case, but MoveToEndOfLine() deselects all text again no matter which parameters I pass to it.

    Code (CSharp):
    1.     private async void PlaceCursor() {
    2.         inputField.Select();
    3.         inputField.ActivateInputField();
    4.         await System.Threading.Tasks.Task.Delay(10);
    5.         inputField.MoveToEndOfLine(shift: true, ctrl: false);
    6.     }
    Increasing the delay makes it possible to observe the behavior rather well. All text is selected until the cursor is placed.

    input_deselected_text.gif
     
  11. Andefob

    Andefob

    Joined:
    Sep 17, 2018
    Posts:
    99
    I bumped into this old thread while trying to get my text input to work as intended. (With Unity 2021.1.25)

    The problem is that I try to activate and set the text position in the same tick. When I call MoveTextEnd after activating the text input object I can see in the debugger that text is correct and text.length is non-zero. However, as I don't have rich text editing enabled, position is determined by this, which results in -1: "int position = m_TextComponent.textInfo.characterCount - 1"

    If I set rich text editing true (which I definitely don't want to do), the position setting is taken from text.length which works and makes the caret be at the end. I fixed this with an ugly hack of setting the position again in the next tick.

    As a sidenote, I find it confusing the m_isRichTextEditingAllowed part of the if is simpler than the non-rich-text version but maybe there is a good reason for that... This is the function as shown in my debugger:
    Code (csharp):
    1.  
    2.         public void MoveTextEnd(bool shift)
    3.         {
    4.             if (m_isRichTextEditingAllowed)
    5.             {
    6.                 int position = text.Length;
    7.                 if (shift)
    8.                 {
    9.                     stringSelectPositionInternal = position;
    10.                 }
    11.                 else
    12.                 {
    13.                     stringPositionInternal = position;
    14.                     stringSelectPositionInternal = stringPositionInternal;
    15.                 }
    16.             }
    17.             else
    18.             {
    19.                 int position = m_TextComponent.textInfo.characterCount - 1;
    20.                 if (shift)
    21.                 {
    22.                     caretSelectPositionInternal = position;
    23.                     stringSelectPositionInternal = GetStringIndexFromCaretPosition(position);
    24.                 }
    25.                 else
    26.                 {
    27.                     caretPositionInternal = caretSelectPositionInternal = position;
    28.                     stringSelectPositionInternal = stringPositionInternal = GetStringIndexFromCaretPosition(position);
    29.                 }
    30.             }
    31.             UpdateLabel();
    32.         }
    33.  
     
    YagirX and x4000 like this.