Search Unity

insert special character Input field

Discussion in 'Scripting' started by danicrem, Sep 7, 2020.

  1. danicrem

    danicrem

    Joined:
    Nov 9, 2014
    Posts:
    45
    Hello,
    I want to create a button that inserts a special character at the caret position in the Text Mesh Input field. It should also work with some text already present in the field (so add a character to an existing string at the position of the caret in the Input field).

    Though I can't find any code examples. So help is welcome!
     
    Last edited: Sep 7, 2020
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I haven't done this, but can describe generally how you probably want to approach it.

    TMP_InputField.caretPosition should tell you where the cursor is within the field. Then you should be able to access the text property, use String.Substring to split it up at that position, and stitch it back together with whatever you want in the middle.

    Note that pressing an on-screen button will deselect the input field, which might erase the caretPosition (not sure). If so, you'd have to monitor it proactively and remember the last value so that you can use it even when the field is no longer selected.

    Also, caret position can be a bit more complicated than you'd think. Remember to test what happens if the user drag-selects a block of text. Also, if your input field supports rich text, you may need to pay attention to the difference between the raw text and the visible text, keeping in mind that position N in one of those doesn't necessary correspond to position N in the other.
     
    rogercsardev likes this.
  3. danicrem

    danicrem

    Joined:
    Nov 9, 2014
    Posts:
    45
    OK, that sounds a lot more complicated than I thought. Perhaps it would be better to focus on a custom virtual keyboard with custom buttons for the additional symbols.