× Phaseshift FCOM Tutorials

Create a typewriter text scrolling effect


How to create a typewriter scrolling effect with TextMeshPro

You can create a quick typewriter scrolling effect with one simple script. For the purposes of this tutorial we're going to assume you have a TextMeshProUGUI component in your UI you would like to use.  

Create a new script in Unity and call it whatever you like (we called it UI_TypeText). Now let's attach this new script to the GameObject that has the TextMeshProUGUI component. Now open this new script. 

The UI text GameObject
Add the script to the GameObject with the TextMeshProUGUI you want to effect

Once in the script, we want to do a couple of things. Firstly we want to reference the TextMeshProUGUI component that the script is attached to. We can do this on Awake().

This bit of code is not needed, but is something that I like to do in case I forget to set the TextMeshProUGUI component in the Inspector! All this does is set the text variable to the TextMeshProUGUI component on the GameObject. This means you can be lazy too, you can just keep adding these scripts to all of your UI text to add typewriter effects with absolutely no set up!

Next, lets write the actual functionality. 

When StartTypeText() is called, it triggers a Coroutine that sets the text that you set in TextMeshProUGUI's inspector, then iterates through each character, adding it to the display then WaitForSeconds() between each character. This causes a typewriter effect.

And...that's it! Use the StartTypeText() method to call the Coroutine from any Script with a reference to this object. You can also choose to start the Coroutine on Start(), OnEnable() etc. This function allows us to manually trigger the effect to give us the most control.

Full code:

Performance

This method is tied to frame rate, so on devices where frame rate may be slower say for example on a mobile phone, the text can show at a slower pace. 

Documentation


To explore more on these topics, check out the documentation:

TextMeshPro
WaitForSeconds

Liked this article?

Please consider sharing!
Author

Josh Lyell

Game Developer