I’m instantiating a GameObject for each letter in a line of poetry so that I can create dynamic effects. The preferred width of the text is used to position the letters.
void SetupText()
{
// instantiate letters
for (int i = 0, max = text.Length; i < max; ++i)
{
string letter = text*.ToString();*
TrickleLetterUI l = Instantiate(letterPrefab).GetComponent();
l.gameObject.transform.SetParent(lettersParent, false);
l.gameObject.name = "Letter: " + letter;
l.Setup(letter);
letters.Add(l);
}
letterPrefab.SetActive(false);
// position letters
for (int i = 0, max = text.Length; i < max; ++i)
{
TrickleLetterUI l = letters*;*
l.letterWidth = l.text.preferredWidth;
Vector3 letterPosition = lettersParent.position;
if (i > 0)
{
TrickleLetterUI prevLetter = letters[i - 1];
letterPosition = prevLetter.transform.position + (prevLetter.transform.right * ((prevLetter.letterWidth / 2) + l.letterWidth / 2));
}
l.transform.rotation = transform.rotation;
l.gameObject.transform.position = letterPosition;
l.Init(i, 0, this);
wordWidth += l.letterWidth;
}
}
The GameObjects each have Text components and are children of a Canvas with a Canvas Scaler (UI Scale Mode set to Scale with Screen Size). When my Game window is set to the reference resolution (800x600), the text is positioned correctly:
[115951-textappearreferenceresolution.png|115951]_
When my Game window is set to Maximize On Play, or sized differently than the reference resolution, the text isn’t positioned correctly:
_[115952-textmaximizeonplay.png*|115952]*
_*
_*