Other
Text letter-by-letter dialogue overflow
I have a dialogue system where letters in a text box show up one by one. The issue is that since letters are added individually to a string, a word that would originally overflow to the next line stays at the top line until all of its letters are loaded. Check out what I mean:
As you can see in that gif, the word "message" sits on the top line until enough letters are loaded into the string for it to overflow. Is there any way to prevent this? A way to detect if a word will overflow ahead of time perhaps?
Answer by kellerr112 · Jan 22, 2016 at 07:56 PM
Yes. You can do this by lerping the string's length from 0 to n.
Here's some pseudocode:
string str = "I am being lerped right now"; // the string you want
public Text UItext // the text you want to apply the string to
void Update()
{
UItext.text = str;
}
IEnumerator AnimateText(float animationTime = .5f)
{
float elapsed = 0;
string targetString = str;
string startingString = "";
while(elapsed < animationTime)
{
elapsed += Time.deltaTime;
float t = elapsed/animationTime;
str = targetString.Substring(0, Mathf.FloorToInt(targetString.Length * t) ) // take the first n letters.
yield return new WaitForEndOfFrame();
}
}
Same issue when using Substring. In the gif, notice how words like "right" and "lerped" will start off on one line but end up on the following line after the characters load in.