Advertisement
Sabotender

Typewriter text effect Unity

Nov 5th, 2014
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.99 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.UI;
  3.  
  4. public class Typewriter : MonoBehaviour {
  5.     public float timeBetweenCharacters = 2;
  6.  
  7.     private Text textField;
  8.     private string originalText;
  9.     private int position = 0;
  10.     private float time;
  11.    
  12.     void Start() {
  13.         textField = GetComponent<Text>();
  14.         originalText = ParseText(textField.text);
  15.         textField.text = "";
  16.     }
  17.  
  18.     string ParseText(string text) {
  19.         string t = text;
  20.         t = t.Replace("[device]", SystemInfo.deviceName);
  21.         t = t.Replace("[name]", "Felix");
  22.         return t;
  23.     }
  24.    
  25.     void Update () {
  26.         if(position < originalText.Length && time <= 0) {
  27.             char c = originalText[position];
  28.  
  29.             if(c == '|') {
  30.                 textField.text = textField.text.Substring(0,textField.text.Length-1);
  31.             } else if (c == '[') {
  32.                 textField.color = Color.red;
  33.             } else if (c == ']') {
  34.                 textField.color = Color.white;
  35.             } else {
  36.                 textField.text += c;
  37.             }
  38.  
  39.             position++;
  40.             time = timeBetweenCharacters;
  41.         }
  42.         if(time > 0)
  43.             time -= Time.deltaTime;
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement