Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[TMP] Changing the text and caret position in the same frame

Discussion in 'UGUI & TextMesh Pro' started by QFSW, Jun 17, 2019.

  1. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Is it possible to change the text of an input field, and then move the caret position to the end in one frame?

    I am trying this which works for UGUI but not for TMP

    Code (CSharp):
    1. ConsoleInput.text = newInput;
    2. ConsoleInput.Select();
    3. ConsoleInput.caretPosition = newInput.Length;
    4. ConsoleInput.selectionAnchorPosition = newInput.Length;
    5. ConsoleInput.ActivateInputField();
    Thanks!
     
  2. Stephan_B

    Stephan_B

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

    The following example works as expected on my end using the latest releases.

    Code (csharp):
    1.  
    2. InputFieldComponent.text = "A line of text";
    3. InputFieldComponent.Select();    
    4. InputFieldComponent.MoveTextEnd(false);
    5. InputFieldComponent.ActivateInputField();
    6.  
     
  3. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Thank you, MoveTextEnd works. Is it a bug that moving the caret manually doesn't?
     
  4. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    I tested both on my end and that also appears to work as expected.

    Do you have an example script that I could use to try to reproduce the behavior you are seeing?
     
  5. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    The code in my initial post doesn't work properly and only pushes it to the end of the current (previous) text. 2018.2.12f1
     
  6. Stephan_B

    Stephan_B

    Joined:
    Feb 26, 2017
    Posts:
    6,595
    Since I made a lot of changes / improvements to the TMP Input Field in version 1.4.x, I would suggest testing (just this) in Unity 2018.4 with the latest release of TMP which is verison 1.4.1 to make sure it behaves as expected.

    Assuming it does, it might make sense if you can to migrate to Unity 2018.4 with the latest release of TMP. But step one is making sure you get the desired behavior in the latest release.
     
  7. QFSW

    QFSW

    Joined:
    Mar 24, 2015
    Posts:
    2,906
    Migrating isn't much I an option since I'm developing plugins, but I'll test it out!