Search Unity

TextMesh Pro MoveTextEnd(false) seems to behave strangely

Discussion in 'UGUI & TextMesh Pro' started by Gladyon, Jul 29, 2021.

  1. Gladyon

    Gladyon

    Joined:
    Sep 10, 2015
    Posts:
    389
    I need to place the caret at the end of an Input Field which contains several lines.

    MoveToEndOfLine(false, false) does what it should, it goes at the end of the line, not at the end of the text

    MoveTextEnd(false) places the caret at the start of the text (the name of the method may lead to some interpretation errors...)
    Note that MoveToEndOfLine(true) will select the whole text, all lines, a bit like if we selected up to the end of the text with 'Shift', which is what it should do if I am not mistaken.

    I can move the caret manually by setting caretPosition to text.Length, but I don't think it's the 'correct' way to do it.
    I am pretty sure that the MoveTextEnd(false) method has a problem, maybe it's because I filled and selected the input field just before trying to move the caret:
    Code (CSharp):
    1. NativeText.text = "This is a long line just to be sure there are several lines in the Input Field so I can check if moving the caret at the end of the text actually work or not";
    2. NativeText.Select();
    3. NativeText.MoveTextEnd(false);
     
  2. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    What version of the TMP package are you using?

    Since you just set the text, the text component hasn't yet updated the text itself and therefore, the MoveTextEnd would be acting on the previous text.

    See if adding the following additional line to your code resolves the issue.

    Code (csharp):
    1.  
    2. ativeText.text = "This is a long line just to be sure there are several lines in the Input Field so I can check if moving the caret at the end of the text actually work or not";
    3. NativeText.Select();
    4. NativeText.MoveTextEnd(false);
    5. // Add this new line.
    6. NativeText.ActivateInputField();
     
  3. Gladyon

    Gladyon

    Joined:
    Sep 10, 2015
    Posts:
    389
    3.0.6, with Unity 2020.2.4

    That's what I thought first, but since MoveToEndOfLine(true) selects up to the end I don't think it's the problem.
    Also, using this work:
    Code (CSharp):
    1. NativeText.caretPosition = NativeText.text.Length;
    If it's an update problem I don't think it would work.

    Just tried it, still the same, the caret is still at the start.