Sorry if this has been asked a bunch. I can’t seem to find anything when I do a search.
Is there a way to take blocks of text and have sentences printed to the screen one letter at a time?
Sorry if this has been asked a bunch. I can’t seem to find anything when I do a search.
Is there a way to take blocks of text and have sentences printed to the screen one letter at a time?
Just watch will goldstones UI videos on the unity blogs
http://blogs.unity3d.com/2014/06/30/unity-4-6-new-ui-world-space-canvas/
The only issue with that technique (String reconstruction / concatenation) is that it doesn’t work with Word Wrapping and Rich Text Tags. If you can live with those limitations and the GC generated by the string concatenation, then you are all set. However, if you cannot live with those limitations, here is an alternative solution which does that and a lot more.
https://www.youtube.com/watch?v=U85gbZY6oo8
P.S. The same reveal effect can be done with text objects that contain text and sprites.
If you have the budget, buy TextMesh Pro. It’ll make your text look amazing, and it’s super easy to use.
It’s still possible in Unity UI, but it’s a bit of work. If I had no other option, I think I’d prefer to mow a few lawns for cash to buy TextMesh Pro.
Otherwise, you’ll have to parse through the string and jump forward past rich text tags, keeping track of them on a stack so you can close them. For example, say your text is:
Oh my!
As you go through the string (probably in a coroutine), you’ll change the text element’s content to this:
O
Oh
Oh_
Oh_ [Put on the stack, and append to properly close it]
Oh_m
Oh_my [We hit , so pop from stack]
Oh_my!
You also need to precompute line breaks before adding characters. If the text would word wrap between “Oh” and “my” but not between “Oh” and “m”, make sure you don’t set the text to “Oh_m”. In this case, you need to temporarily insert a newline character: “Oh_\nm”.
In addition, if you use Center or Right alignment, you need to precompute the length of each line so you can manually position each one. Otherwise, if you use Center alignment for example and don’t position each line for its full length, the text will appear to grow from the center and expand out, which doesn’t look right.
You can use Font.GetCharacterInfo() to precompute line breaks and positions.
Another solution might be to use Font.GetCharacterInfo() to get the size of all characters, then move and/ or scale a mask as neccessary. So it could be: yield => Font.GetCharacterInfo(nextChar, …) => move mask by characterInfo.advance => repeat. If you have a managable amount of fonts and styles, you could also create a Dictionary beforehand to store each character’s width.
+1million for @Stephan-B solution
Since we are stuck with the legacy text system, assets like TextMeshPro are a lifesaver.
It’s also more performant, especially if you have lots of text