Unity3D – Scrolling Typewriter effect – JavaScript

Create a scene with:

– TextMesh
– Empty Object, assign TypeWriter.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#pragma strict
 
var displayText : TextMesh;
 
var textShownOnScreen: String ;
var fullText : String = "The text you want shown on screen with typewriter effect.";
var wordsPerSecond : float = 2; // speed of typewriter, words per second
var timeElapsed : float = 0;  
  
function Update()
{
    timeElapsed += Time.deltaTime;
    textShownOnScreen = GetWords(fullText, timeElapsed * wordsPerSecond);
    displayText.text = textShownOnScreen;
}
  
function GetWords(text : String, wordCount : int): String
{
    var words : int = wordCount;
  
    // loop through each character in text
    for (var i : int = 0; i < text.Length; i++)
    {
        if (text[i] == ' ')
        {
            words--;
        }
  
        if (words <= 0)
        {
            return text.Substring(0, i);
        }
    }
  
    return text;
}

Inspector, assign a 3DText to var displayText.