- Home /
Scrolling typewriter effect
I would like to make a game where the text already been defined, and when I run the game or program, it will display the text, but not fully text on once, but as one word by words. Something like this:
http://wigflip.com/screedbot/ and http://www.youtube.com/watch?v=uFQ6MspAMq8 (The youtube video is already give the example, but it is on c# xna)
Answer by Laik · Jun 16, 2015 at 05:43 AM
Here is one i wrote that i feel works better:
public Text textBox;
//Store all your text in this string array
string[] goatText = new string[]{"1. Laik's super awesome custom typewriter script", "2. You can click to skip to the next text", "3.All text is stored in a single string array", "4. Ok, now we can continue","5. End Kappa"};
int currentlyDisplayingText = 0;
void Awake () {
StartCoroutine(AnimateText());
}
//This is a function for a button you press to skip to the next text
public void SkipToNextText(){
StopAllCoroutines();
currentlyDisplayingText++;
//If we've reached the end of the array, do anything you want. I just restart the example text
if (currentlyDisplayingText>goatText.Length) {
currentlyDisplayingText=0;
}
StartCoroutine(AnimateText());
}
//Note that the speed you want the typewriter effect to be going at is the yield waitforseconds (in my case it's 1 letter for every 0.03 seconds, replace this with a public float if you want to experiment with speed in from the editor)
IEnumerator AnimateText(){
for (int i = 0; i < (goatText[currentlyDisplayingText].Length+1); i++)
{
goatTalkingText.text = goatText[currentlyDisplayingText].Substring(0, i);
yield return new WaitForSeconds(.03f);
}
}
And that's it! Works perfectly well and doesn't clog up Update
This is great, thank you! $$anonymous$$y next step for my game is to figure out how to predict the number of lines of text that will be displayed on the screen, and center the text vertically in my dialog box prior to all of it being there. Right now, it re-centers as the typehead gets to the second line.
Answer by patrik-org · Mar 31, 2014 at 01:22 PM
I'm assuming you already know how to display text on the screen. Next you need a script that updates this text every frame:
public string textShownOnScreen;
public string fullText = "The text you want shown on screen with typewriter effect.";
public float wordsPerSecond = 2; // speed of typewriter
private float timeElapsed = 0;
void Update()
{
timeElapsed += Time.deltaTime;
textShownOnScreen = GetWords(fullText, timeElapsed * wordsPerSecond);
}
private string GetWords(string text, int wordCount)
{
int words = wordCount;
// loop through each character in text
for (int i = 0; i < text.Length; i++)
{
if (text[i] == ' ')
{
words--;
}
if (words <= 0)
{
return text.Substring(0, i);
}
}
return text;
}
The above code updates the textShownOnScreen string to get a typewriter effect. You might want to modify GetWords() in how it counts words - right now any space it encounters in the text it interprets as being a new word.. I'm sure there are better ways but this might help you get started.
Hey @exmakina , even though it isn't marked as solved this helped me out quite a bit :P
However it says I can't convert a float to int... (on the textShownOnScreen = GetWords(fullText, timeElapsed * wordsPerSecond); part)... I tried creating an int for that but it only gets changed every second so it's weird...
But great and simple answer :)
Answer by USMANHEART · Dec 21, 2016 at 09:41 AM
If you want to show On your textbox also alphabet per frame, then follow this
public class Testing : MonoBehaviour {
public string textShownOnScreen;
public Text textBox;
public string fullText = "The text you want shown on screen with typewriter effect.";
public float wordsPerSecond = 2; // speed of typewriter
private float timeElapsed = 0;
void Update() {
timeElapsed += Time.deltaTime;
textShownOnScreen = GetWords(fullText, timeElapsed * wordsPerSecond);
}
private string GetWords(string text, float wordCount) {
float words = wordCount;
// loop through each character in text
for (int i = 0; i < text.Length; i++)
{
words--;
textBox.text = textShownOnScreen;
if (words <= 0)
{
return text.Substring(0, i);
}
}
return text;
} }
Answer by Stoneheart · Mar 10, 2015 at 02:40 PM
In the provided code do this: public float wordsPerSecond = 2f; // speed of typewriter private float timeElapsed = 0f;
Answer by MetalStorm80 · Jun 14, 2015 at 07:31 AM
This was very handy, I still don't get the return text.Substring(0, i); and return text; but it works and I will slowly figure out why.
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
A node in a childnode? 1 Answer
Multiple Cars not working 1 Answer
How to display text on a cube and how to change it dynamically? 2 Answers