Search Unity

  1. Unity 2019.1 beta is now available.
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. We're looking for insight from anyone who has experience with game testing to help us better Unity. Take our survey here. If chosen to participate you'll be entered into a sweepstake to win an Amazon gift card.
    Dismiss Notice
  4. On February 28th the Feedback website will shut down and be redirected to the Unity forums. See the full post for more information.
    Dismiss Notice
  5. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    Dismiss Notice
  6. Unity 2018.3 is now released.
    Dismiss Notice
  7. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice

Textmeshpro Caret Position Change

Discussion in 'Unity UI & TextMesh Pro' started by SAJID_Playsimple, Apr 10, 2018.

  1. SAJID_Playsimple


    Apr 10, 2018
    i'm trying to move caret in textmeshpro inputfeild To A Certain Position In A String of 10 letters
    but it is not working for some reason i've used the latest version of textmeshpro

    I've logged the position and it is accurate but visually the cursor stays at the same place
    can anyone help me on how to move caret in inputeild manually via script

    public TMP_InputField MessageInputField;
    void SomeFuntion(){
    MessageInputField.caretPosition = MessageInputField.text - 5;
  2. PSchaussPlaya


    Mar 2, 2018
    Did you find any solution by now?

    I have the same problem with Unity 2018.1.0f2 and TMP 1.2.2 from the package system.
    Setting caretPosition does not seem to have any impact on the caret position.
  3. PSchaussPlaya


    Mar 2, 2018
  4. troien


    Jan 15, 2013
    I managed to get something like this to work. Although it is a workaround...

    I wanted to make another UI item (which is linked to a part of the text) to select the correct piece of text when I hover the other UI item.

    Something like this is what I ended up using on that other UI item.

    Code (CSharp):
    1. public class ExampleClass : Selectable, IPointerEnterHandler, IPointerExitHandler
    2. {
    3.     [SerializeField]
    4.     private TMP_InputField field;
    6.     public override void OnPointerEnter(PointerEventData eventData)
    7.     {
    8.         field.selectionAnchorPosition = 1; // your caret start position here
    9.         field.selectionFocusPosition = 4; // your caret end position here
    10.         field.Select();
    11.         this.Select();
    12.     }
    14.     public override void OnPointerExit(PointerEventData eventData)
    15.     {
    16.         field.selectionAnchorPosition = field.selectionFocusPosition;
    17.         field.Select();
    18.         this.Select();
    19.     }
    20. }
    I don't know whether this will work for you because you have a different setup. But when I added the 2 selects, it started to work as intended. So I think this might work for you aswell, provided you have something else to select.
  5. lipisis


    Jan 14, 2017
    The original problem you posted is that you are trying to subtract integer from a string. In most basic case something like this should do the job:
    Code (CSharp):
    1. // Move caret to the given position
    2. void MoveCaretTo(int pos){
    3.   inputfield.caretPosition = pos;
    4. }
    There are two attributes:
    caretPosition - represents caret position ignoring rich text tags
    stringPosition - represents caret in the original string (tags included)

    Personally I've had some issues that after adding some tags, visually caret jumps to other place even if I didn't modify any of these attributes. I've done some debugging and I think that actual caret position that user sees is based on
    . I had do modify this attribute so user won't feel the difference and caret will stay in same place because caretPosition is (in my opinion) based on stringPosition. Fun thing is that caretPosition updates only after user modifies input so it is possible to jump into situation that modifying caretPosition does not do anything.