How to determine string location inside a Text

Hi everyone!

I’m trying to develop a quiz game with the classic fill th gaps method.

I’ve a database with some questions with the gaps represented as a “_____” pattern, and I want to create a input field on top of it.

The problem is that since the position of the pattern is not fixed, I have lo locate it.

Since the line height is fixed, was easy to locate the y coordinate, but not the x one (of the first ‘_’ char). I tried several ways, but with no success.

  • I tried to get chars width to generate a left offset with:
    textObj.cachedTextGenerator.characters [charPosition].charWidth

  • I tried to locate the char with
    textObj.cachedTextGenerator.characters [charPosition].cursorPos.x

  • I tried to get a monospace font, but it messed all

Has anyone done something similar?

Thank you very much!!

EXAMPLE:

2 + _ = 5

Here I want to locate the x position of the ‘_’ char to create an input field on top.

how come you use five different texts for all the digits in your example.
and check the value in the texts if it is ‘', you can instantiate an input field over there.
if there are multiple '
’ in your questions , it might help you there instead of taking x, y positions.

Hello everyone!

I already solved my problem. I posted the code on https://forum.unity.com/threads/how-to-determine-string-location-inside-a-text.518585/#post-3399036 just in case someone has the same needs.

Probably there is a better and more elegant way to achieve it, but one works xD

Thank you @upasnavig90 for your help!