Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics

[Solved?] inserting programmatically into the ​current position of the caret, getting the current position of the caret

A topic by created Mar 06, 2018 Views: 267 Replies: 5
Viewing posts 1 to 6

When Super Text Mesh is in the editable mode, Is it possible to insert text/rich text pro grammatically into the current position of the caret? Is there an API to get the current position of the caret ? Thanks!



I'm not 100% sure what you mean, so I'm guessing you're making a text entry field in your game?

Ignoring rich text, the position of a character relative to the object's origin should be just[indexOfCharacter].pos; You can use this to draw a caret in the right spot!


Suppose we have a string "0123456789" and the caret is currently at '3'.  Is there a something similar to insertTextAtCaretPosition("***") 

or insertTextAt(getCaretPosition(), "***")

which will result in "012***3456789"




Super Text Mesh is just meant to be a text renderer, so the type of code that would make this work for Unity's standard mesh will also work for STM.

If the caret is currently at index 3, then... 

superTextMesh.text = superTextMesh.text.Insert("***", caretPosition);

should insert a string at the caret's position.


Thanks! How can I get the caret position (in characters)? 

Developer (1 edit)

Super Text Mesh is just a text renderer, not a text editor, so it doesn't have a caret it keeps track of by itself. To edit text at a certain position, you have to keep track of the "caret" with a variable.

Here's some pseudocode of how you can do this:

public int caretPosition = 0;  public SuperTextMesh stm; public void InsertTextAtCaret(string newText){      //add the string to super text mesh's text variable at the caret's position     stm.text = superTextMesh.text.Insert(newText, caretPosition);

To reiterate, this type of code can run in any string, not just Super Text Mesh's "text" variable!