- Home /
Positions of UI Text characters.
What I'm trying to do is draw a Rect around substrings of a UI Text component using href markup. What actually happens is this:
(The red boxes should be around the 'Click Me!:' segments of text)
Here's the code for it, which is within a class derived from UnityEngine.UI.Text:
public void CreateHrefs(){
currentText = this.text;
Debug.Log ("Creating Hrefs");
StringBuilder returnString = new StringBuilder ();
int index = 0;
foreach (Match match in s_HrefRegex.Matches(currentText)) {
returnString.Append (currentText.Substring (index, match.Index - index)); // Everything before it.
if (!hrefs.ContainsKey(match.Groups[1].Value)) {
HrefInfo hrefInfo = new HrefInfo {
startIndex = (returnString.Length * 4),
endIndex = (returnString.Length + match.Groups [2].Length - 1) * 4 + 3,
href = match.Groups [1].Value,
name = match.Groups [2].Value
};
TextGenerator gen = this.cachedTextGenerator;
UIVertex[] vertices = gen.GetVerticesArray ();
Debug.Log ("<color=red>Href Indexes: " + hrefInfo.startIndex + " - " + hrefInfo.endIndex + "</color>");
Vector3 startPos = vertices [hrefInfo.startIndex].position / canvas.scaleFactor;
Vector3 endPos = vertices [hrefInfo.endIndex].position / canvas.scaleFactor;
float _x = startPos.x;
float _y = startPos.y;
float xSize = endPos.x - startPos.x;
float ySize = endPos.y - startPos.y;
Rect rect = new Rect (_x, _y, xSize, ySize);
Color color = Color.red;
Vector2 topLeft = new Vector2 (rect.xMin, rect.yMin);
Vector2 bottomLeft = new Vector2 (rect.xMin, rect.yMax);
Vector2 topRight = new Vector2 (rect.xMax, rect.yMin);
Vector2 bottomRight = new Vector2 (rect.xMax, rect.yMax);
topLeft = transform.TransformPoint (topLeft);
bottomLeft = transform.TransformPoint (bottomLeft);
topRight = transform.TransformPoint (topRight);
bottomRight = transform.TransformPoint (bottomRight);
Debug.DrawLine (topLeft, topRight, color, 999F, false);
Debug.DrawLine (topRight, bottomRight, color, 999F, false);
Debug.DrawLine (bottomRight, bottomLeft, color, 999F, false);
Debug.DrawLine (bottomLeft, topLeft, color, 999F, false);
hrefInfo.rect = rect;
Debug.Log ("Href detected::" + hrefInfo.name + "::" + hrefInfo.href + " Verts::" + startPos.ToString () + "::" + endPos.ToString () + "::");
hrefs.Add (hrefInfo.href, hrefInfo);
}
index = match.Index + match.Length;
returnString.Append (match.Groups[2].Value);
}
returnString.Append(currentText.Substring(index, currentText.Length - index));
outputText = returnString.ToString ();
this.text = outputText;
}
Trying to resolve the issue, I've tried removing the differing text sizes, disabling richtext entirely, switching between a new instance of a textgenerator and the cached one, and looking at how other people have done it. I've tried using a couple of free asset packages, but they seem to suffer similar issues. The text I'm putting into it isn't anything special;
<size=30><b>Example</b></size>
<size=20><b>Example Href: </b></size>
<size=17.5>....Click Me!: <color=75CF1A>30</color></size>
<size=17.5>....Click Me!: <color=75CF1A>30</color></size>
<size=20>..Other: 0</size>
If anyone has any clue where I've gone wrong, I would love the help.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
To see chosen random text UI Unity,To Access UI text 0 Answers
UI Text - changing material instance 1 Answer
Unity 4.6 Text Misbehaves During Gameplay 1 Answer
[Solved] UI Text not updating C# 3 Answers