My goal is to place buttons over certain words in text (ie, hyperlinking).
I’m close, but I can’t seem to get the height position correct. It’s just slightly off.
I believe my problem must lie in this line?
newC.y= _Text.cachedTextGenerator.GetPreferredHeight(thisStr, tgs);
Using Regex, I grab all the pertinent links and the iterate through them to Instantiate the buttons.
This switch case handles that for me:
case "href":
rt = container.GetComponent<RectTransform> ();
float containerWidth = Screen.width - rt.sizeDelta.x - rt.anchoredPosition.x; //this is stretched so keep testing
containerWidth -= container.parent.GetComponent<RectTransform> ().sizeDelta.x;
containerWidth -= container.parent.GetComponent<RectTransform> ().anchoredPosition.x;
containerWidth -= 20; //Text box itself
CharacterInfo info = new CharacterInfo ();
string thisStr = "";
Vector2 newC = Vector2.zero;
//get the X position of the button. My example text should produce all zeros here.
for (int j = 0; j < _Text.text.IndexOf (match.Groups[19].Value); j++) {
thisStr += _Text.text[j].ToString ();
if (_Text.text[j].ToString () == "\n" || _Text.text[j].ToString () == "\r") {
newC.x = 0;
continue;
}
instance.font.RequestCharactersInTexture (_Text.text[j].ToString (), 18);
if (instance.font.GetCharacterInfo (_Text.text[j], out info, 18)) {
if (newC.x + info.advance > containerWidth)
newC.x = info.advance;
else
newC.x += info.advance;
}
}
TextGenerationSettings tgs = _Text.GetGenerationSettings (new Vector2 (containerWidth, 1000)); //arbitrary height for now
newC.x += 10; //account for Text.text indent
newC.y = _Text.cachedTextGenerator.GetPreferredHeight(thisStr, tgs);
newC.y *= -1; //string down from top
tmp = (GameObject)Instantiate((GameObject)Resources.Load ("Prefabs/btn_Url"), Vector3.zero, Quaternion.identity);
tmp.transform.SetParent (container);
rt = tmp.GetComponent<RectTransform> ();
rt.anchoredPosition = newC;
//finally let's set the button size
newC = Vector2.zero;
newC.x = _Text.cachedTextGenerator.GetPreferredWidth(match.Groups[19].Value, tgs);
newC.y = _Text.cachedTextGenerator.GetPreferredHeight(match.Groups[19].Value, tgs);
rt.sizeDelta = newC;
break;
My example Text.text looks like this:
My Text settings:
Font Arial
Style Normal
Size 18
Line Spacing 1
Rich Text true
Alignment upper left
Wrap & Truncate
Best Fit false
Material None
And the resulting placement is attached as an image. It shows that:
- all the buttons are the correct size
- the initial button positions appear to be correct
- then the positions of the text appear to severely come up short in regards to height.

