- Home /
The question is answered, right answer was accepted
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.
Could you upload your code, That might clarify the question
Answer by belwar · Feb 22, 2018 at 08:57 AM
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!
Answer by upasnavig90 · Feb 21, 2018 at 12:28 PM
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.
Actually, the questions are much more complex, so I would have to create a lot of texts, right?
(Sorry, I don´t know how to indent the code here)
For example:
01 public class Perro {
02 public static void ladrar(){
03 System.out.print("Ladrando ");
04 }
05 public void correr(){
06 System.out.print("Corriendo ");
07 }
08 }
09 public class $$anonymous$$ainPerro {
10 public static void main(String[] args) {
11 Perro.ladrar();
12 Perro p = new __();
13 p.ladrar();
14 p.correr();
15 }
16 }
Thank you very much for your reply!
Sorry, I dnt know exactly, but here is one idea if you want to try: split the text by '', the put first part in one text the , put inputfield for '' after the textbox (by taking length of textbox), then text box for next texts.
Hum... But where I instantiate the input field then? I need the position where the field have to be spawn, right?
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
How to drag objects from UI into scen 3 Answers
Overlay 2 scenes 1 Answer
UI dropdown index problem 1 Answer