- Home /
Char-by-char RPG style textbox + word wrapping...
Kinda new here still. Anyway, I've successfully made a textbox method that takes a string, and then draws it one character at a time at x speed with the traditional bloopy sound, etc... The issue is that Unity's built-in word wrap will cause it to draw half the word and then teleport the whole thing to the next line abruptly when it goes over the border.
Currently I'm using a fixed-width font, and I just feed it the character width in pixels, count chars to tell if it's gone over, and then insert \n at the nearest whitespace or hyphen. I'd really like this script to work with any font though, especially variable width fonts... Even with fixed width, there's already a problem where the spaces are narrower than the characters, and I really don't wanna count the width of every individual character.
I've thought about using GUIStyle.CalcSize, but I still need to tell it where in the string to put the linebreaks. Any suggestions?
Answer by sschaem · Jun 23, 2014 at 10:48 PM
One way to do it is always render your entire final text (so wrapping doesn;t change), and you instead insert a color tag after the last character you want to display. ex: If you have a black background, and you text is white, simply add a color tag to make your text black after the last char you want to display.
Video of how its done in "TextMesh Pro" using its built in functionality for reveals. https://www.youtube.com/watch?v=ToTOrYolVuc
Ahaha... Clever hack... I guess it wouldn't work if I had a textured background or something though.
I should look into using Text$$anonymous$$esh Pro. Is it much different from using UnityGUI?
It follows Unity Textmesh closely : here is the online API http://digitalnativestudios.com/textmeshpro/docs/ScriptReference/Text$$anonymous$$eshPro.html
65 dollars, jeez. Isn't there a free way to make characters invisible? Would it be possible to extend UnityGUI and add a hide tag?
http://docs.unity3d.com/$$anonymous$$anual/StyledText.html
Check the color tag and use it as I outlined in my original response.
It just feels wrong making a script that'll only work on solid color backgrounds... That might be the most practical option though...
Answer by Bunny83 · Jun 22, 2014 at 12:36 AM
Well, this is a very special case which can't be solved that easy i think. You might be able to use GUIStyle.GetCursorStringIndex to detemine the character index where it would wraparound and use that information to insert a newline there.
Your answer
Follow this Question
Related Questions
uGUI Text wrong chinese line break 2 Answers
Predict Text Width of String 0 Answers
Text RPG inventory 1 Answer
Wrapping dynamic text around an object. 1 Answer
How to determine how much of string fits in UI.Text element? 1 Answer