Hello everybody !
I would need a little help ^^
I have a Text component in which I write a string in play mode.
I would like my text to be displayed in “typewriter” mode therefore letter by letter, without the tags being displayed …
I have several dialogues in my project, it might do a lot of processing?
I have for example a sentence in a dialog like this:
<color=#FF0000>Hello everyone</color>, my name is <b><i>John</i></b>! nice to <b>meet</b> you !
for an expected result :
Hello everyone(<== this text in red color), my name is John! nice to meet you !
of course, still in “typewriter” without the tags being displayed while the text is displayed
I have a track here: Typewriter with coloured text - Questions & Answers - Unity Discussions
but the publication dates from 2014, can it be easier to do in 2020?
I asked the question here too: https://forum.unity.com/threads/text-typewriter-without-displaying-richtext-tag.891742/
someone explained to me but I don’t understand, I’m a junior developer
Thanks for your help
Well this is a tricky problem but I think Kurt-Dekker actually explained his approach pretty well. While there might be slightly other approaches the solution he proposes is one of the simplest solution. The idea is to essentially put each character of the original string into two categories. So a character is either part of the “payload” / content or part of a formatting tag. When you want to create a temp string with a certain “content length” you just iterate through your whole source string and copy all characters over one by one. However we want to keep track of how many content character we have already copied. For this we use a boolean flag to indicate if we’re currently inside a formatting tag or if the character is part of the content / payload. We only increase our written content character count if we are actually writing a content character. If we’re currently copying a character from a formatting tag we don’t count those characters. Once our written content character count reaches the number of character we want to display, we keep iterating through the string but from now on we will ignore content character and don’t copy them over. Though we will continue copying the formatting characters.
So the method he proposes would look something like this:
string GetPartialPayload( string s, int typedSoFar)
var tempStr = new System.Text.StringBuilder(s.Length);
int count = 0;
bool payload = true;
for (int i = 0; i < s.Length; i++)
char c = s*;*
if (c == ‘<’)
payload = false;
if (payload && count < typedSoFar)
else if (!payload)
if (c == ‘>’)
payload = true;
Note that “typedSoFar” only cares about “payload” characters. So if you want to do a type writer animation you would call the method above each time you want to update the text with an increasing limit in the “typedSoFar” parameter. To know when you actually reached the end of the string you either have to use the “GetPayloadLength” method that Kurt posted on the forum, or compare the returned string with your original string.
Just to be clear about what will actually happen. We assume our original string looks like this:
“<color=#FF0000>Hello everyone, my name is John! nice to meet you”
When you call “GetPartialPayload” with a number of “0” you get the string:
which is just the formatting tags without any content / payload. When you pass a value of 1 you get
when you pass a value of 23 you get:
“<color=#FF0000>Hello everyone, my name”
when you pass a value of 49 you get the full string back since there are 49 content characters in your string.
@Bunny83 Thank you for your quick reply =)
I appreciate your explanation, just as I appreciate that of Kurt-Dekker.
I understood for the GetPartialPayload method, on the other hand what I can’t understand (it can seem stupid) is how to manage with a Coroutine, that my text is displayed with formatting but without the tags HTML