Search Unity

  1. Unity 2018.3 is now released.
    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. Want more efficiency in your development work? Sign up to receive weekly tech and creative know-how from Unity experts.
    Dismiss Notice
  4. Build games and experiences that can load instantly and without install. Explore the Project Tiny Preview today!
    Dismiss Notice
  5. Nominations have been announced for this years Unity Awards. Celebrate the wonderful projects made by your peers this year and get voting! Vote here!
    Dismiss Notice
  6. Want to provide direct feedback to the Unity team? Join the Unity Advisory Panel.
    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

    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:
    3
    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:
    3
  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.