- Home /
How to split up a text into substrings of single chars at the exact same position
Hi ppl,
so I'm basically trying to build a highlighting mechanic for texts as it would work on any OS.
In pseudo code something like
If (user holds down MouseButton(0))
hightlight every char the mouse hovers over until MouseButton(0)Up
or
OnMouseButton(0)Down {
store MousePosition
}
OnMouseButton(0)Up {
highlight every char between stored position and this MousePosition
}
Now as I see things I need either one of these:
- every character is a text object of its own with its own collider
- a method that translates the positions of the mouse into positions in the text object, creates 3 substrings (of the stuff before the highlighted part, the highlighted part itself and the stuff after that) and displays them in the same positioning as before
The second one seems pretty complicated so I tackled the first one. For usability I want to be able to enter text in the text component of my text object (I'm in Unity Editor right now) and when I hit start I want my method to cut the text into substrings of exactly 1 char.
While this is no problem, I don't know how to position the generated chars so they match their previous positions exactly no matter what font size I chose.
What I need is an algorithm that gives me an offset for the position depending on font size and width of that particular character in the given font.
Any ideas on that?
What I currently have works to create those 1 char text objects with the same font, size etc. as the given text. I use 3D Text instead of UI Text but for no particular reason.
Transform textParent; //this is simply an empty GameObject holding all my text objects
void Start () {
textParent = GameObject.Find("Texts").transform;
for (int i = 0; i < textParent.childCount; i++)
{
CreateObjectForEveryChar(textParent.GetChild(i));
}
}
void CreateObjectForEveryChar(Transform textVessel)
{
TextMesh textMesh = textVessel.GetComponent<TextMesh>();
char[] text;
float offset = 0f;
if (textVessel.GetComponent<TextMesh>() == null)
{
Debug.Log("Error - no TextMesh found on " + textVessel.ToString());
}
else
{
text = textMesh.text.ToCharArray();
for(int i = 0; i < text.Length; i++)
{
GameObject character = (GameObject) Instantiate(textVessel.gameObject, textVessel.localPosition + Vector3.left * offset, textVessel.localRotation);
//character.transform.SetParent(textVessel); //this actually makes unity freeze everytime I hit play so I'm skipping that for now
character.GetComponent<TextMesh>().text = text[i].ToString();
//offset += ??;
}
}
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Is it possible to script a 3D Text? 1 Answer
Best Practice for Multiple Languages? 6 Answers
Maintaining Text Size Ratio With Screen 0 Answers