Search Unity

Textmeshpro Caret Position Change

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

  1. SAJID_Playsimple

    SAJID_Playsimple

    Joined:
    Apr 10, 2018
    Posts:
    1
    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

    PSchaussPlaya

    Joined:
    Mar 2, 2018
    Posts:
    5
    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

    PSchaussPlaya

    Joined:
    Mar 2, 2018
    Posts:
    5
  4. troien

    troien

    Joined:
    Jan 15, 2013
    Posts:
    7
    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;
    5.  
    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.     }
    13.  
    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

    lipisis

    Joined:
    Jan 14, 2017
    Posts:
    20
    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. }
    P.S.
    There are two attributes:
    caretPosition
    and
    stringPosition
    .
    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
    stringPosition
    . 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.
     
  6. lucasmontec

    lucasmontec

    Joined:
    Apr 7, 2015
    Posts:
    85
    It doesn't. Setting the caret doesn't do absolutely nothing.
     
  7. Stephan_B

    Stephan_B

    Unity Technologies

    Joined:
    Feb 26, 2017
    Posts:
    2,799
    This should be working as I tested it today again.

    What version of TMP are you using?

    Can you provide the full script you are using to test this?
     
  8. kwcae

    kwcae

    Joined:
    May 26, 2017
    Posts:
    31
    I've run into this too, unable to increment or decrement the caret position. (Used on a world space UI control). TextMesh Pro 1.3.0, Unity 2018.3.1ff


    Code (CSharp):
    1.  
    2.      public TMP_InputField lblReadout;
    3.  
    4.     //Remove character to the left of the caret
    5.      public void Backspace() {
    6.             if(lblReadout.caretPosition > 0) {
    7.                 lblReadout.text.Remove(lblReadout.caretPosition - 1, 1);
    8.             }
    9.         }
    10.  
    11.         //Shift the caret to the left
    12.         public void PrevCaretPos() {
    13.             if(lblReadout.caretPosition > 0) {
    14.                 lblReadout.caretPosition = lblReadout.caretPosition--;
    15.             }
    16.         }
    17.       //Shift the caret to the right
    18.         public void NextCaretPos() {
    19.             if(lblReadout.caretPosition < lblReadout.text.Length - 1) {
    20.                 lblReadout.caretPosition = lblReadout.caretPosition++;
    21.             }
    22.         }
    23.