aeroson

Untitled

Nov 16th, 2017
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using System;
  6. using TMPro;
  7.  
  8.  
  9. public class UITextTypewriter : MonoBehaviour {
  10.  
  11. public TextMeshProUGUI uiText;
  12. public AudioClip typeSound1;
  13. public AudioClip typeSound2;
  14.  
  15. [Multiline]
  16. public string textEntered = "";
  17.  
  18. public char[] textEnteredSplit;
  19. public char[] textToPrintSplit;
  20.  
  21. public bool isPrinting;
  22. public bool keepLog;
  23. public bool waitForLineBreak;
  24. public float lineBreakDelay;
  25. public char waitForLineBreakChar;
  26.  
  27. public float charPrintDelay = 0f;
  28.  
  29. public bool useFakeEncryption;
  30. private string[] fakeEncryptionChars;
  31.  
  32. private string s;
  33.  
  34. private Queue<Message> newMsgQueue = new Queue<Message> (100);
  35.  
  36. class Message
  37. {
  38. public TextMeshProUGUI uiText;
  39. public string newMessage;
  40. }
  41.  
  42.  
  43. void Start () {
  44.  
  45.  
  46.  
  47. uiText = GetComponent<TextMeshProUGUI> ();
  48. textEnteredSplit = textEntered.ToCharArray ();
  49. textToPrintSplit = new char[textEnteredSplit.Length];
  50. fakeEncryptionChars = new string[10] { "$", "%", "&","/","\\","}","{","|","<",">" };
  51.  
  52. }
  53.  
  54. void Update () {
  55.  
  56. if (Input.GetKeyDown (KeyCode.Space)) {
  57.  
  58. if (!isPrinting) {
  59.  
  60. textToPrintSplit = new char[textEnteredSplit.Length];
  61. //StartCoroutine (PrintText (textEntered, uiText));
  62. }
  63. }
  64.  
  65. if (!isPrinting && newMsgQueue.Count > 0) {
  66. var m = newMsgQueue.Dequeue();
  67. isPrinting = true;
  68. StartCoroutine (PrintText (m.newMessage, m.uiText));
  69. }
  70. }
  71.  
  72. public void DoPrinting(string newMessage, TextMeshProUGUI uiText) {
  73.  
  74. var m = new Message() {
  75. newMessage = newMessage,
  76. uiText = uiText,
  77. };
  78. newMsgQueue.Enqueue(m);
  79. }
  80.  
  81.  
  82. public IEnumerator PrintText(string newMessageEntered, TextMeshProUGUI uiText) {
  83.  
  84. textEnteredSplit = newMessageEntered.ToCharArray ();
  85. textToPrintSplit = new char[textEnteredSplit.Length];
  86.  
  87. isPrinting = true;
  88.  
  89. for (int i = 0; i < textEnteredSplit.Length; i++) {
  90.  
  91. textToPrintSplit [i] = textEnteredSplit [i];
  92. s = new string (textToPrintSplit);
  93.  
  94. s = s.Insert(i+1 ,"_");
  95.  
  96. if (useFakeEncryption) {
  97.  
  98. s = s.Insert(i,fakeEncryptionChars[UnityEngine.Random.Range(0, fakeEncryptionChars.Length)]);
  99. s = s.Remove (s.Length - 3, 1);
  100. }
  101.  
  102. if (s.Length == s.Length) {
  103.  
  104. //s = s.Insert (s.Length - 1, "\n");
  105. s = s.Remove (s.Length - 1, 1);
  106. }
  107.  
  108. if (keepLog) {
  109.  
  110. uiText.text += s[i];
  111.  
  112.  
  113. } else if (!keepLog) {
  114.  
  115. uiText.text = s;
  116. }
  117.  
  118. if (typeSound1) {
  119.  
  120. //SoundManager.instance.PlaySingle (typeSound1);
  121.  
  122. yield return null;
  123. }
  124.  
  125. if (typeSound1 && typeSound2) {
  126.  
  127. //SoundManager.instance.RandomizeSfx (typeSound1, typeSound2);
  128. yield return 0;
  129. }
  130.  
  131.  
  132.  
  133. if (waitForLineBreak && textToPrintSplit[i] == waitForLineBreakChar) {
  134.  
  135. yield return new WaitForSeconds (lineBreakDelay);
  136.  
  137. }
  138.  
  139. yield return new WaitForSeconds (UnityEngine.Random.Range(0.01f,charPrintDelay));
  140. }
  141.  
  142. isPrinting = false;
  143. yield return null;
  144.  
  145. }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment