Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- using System;
- using TMPro;
- public class UITextTypewriter : MonoBehaviour {
- public TextMeshProUGUI uiText;
- public AudioClip typeSound1;
- public AudioClip typeSound2;
- [Multiline]
- public string textEntered = "";
- public char[] textEnteredSplit;
- public char[] textToPrintSplit;
- public bool isPrinting;
- public bool keepLog;
- public bool waitForLineBreak;
- public float lineBreakDelay;
- public char waitForLineBreakChar;
- public float charPrintDelay = 0f;
- public bool useFakeEncryption;
- private string[] fakeEncryptionChars;
- private string s;
- private Queue<Message> newMsgQueue = new Queue<Message> (100);
- class Message
- {
- public TextMeshProUGUI uiText;
- public string newMessage;
- }
- void Start () {
- uiText = GetComponent<TextMeshProUGUI> ();
- textEnteredSplit = textEntered.ToCharArray ();
- textToPrintSplit = new char[textEnteredSplit.Length];
- fakeEncryptionChars = new string[10] { "$", "%", "&","/","\\","}","{","|","<",">" };
- }
- void Update () {
- if (Input.GetKeyDown (KeyCode.Space)) {
- if (!isPrinting) {
- textToPrintSplit = new char[textEnteredSplit.Length];
- //StartCoroutine (PrintText (textEntered, uiText));
- }
- }
- if (!isPrinting && newMsgQueue.Count > 0) {
- var m = newMsgQueue.Dequeue();
- isPrinting = true;
- StartCoroutine (PrintText (m.newMessage, m.uiText));
- }
- }
- public void DoPrinting(string newMessage, TextMeshProUGUI uiText) {
- var m = new Message() {
- newMessage = newMessage,
- uiText = uiText,
- };
- newMsgQueue.Enqueue(m);
- }
- public IEnumerator PrintText(string newMessageEntered, TextMeshProUGUI uiText) {
- textEnteredSplit = newMessageEntered.ToCharArray ();
- textToPrintSplit = new char[textEnteredSplit.Length];
- isPrinting = true;
- for (int i = 0; i < textEnteredSplit.Length; i++) {
- textToPrintSplit [i] = textEnteredSplit [i];
- s = new string (textToPrintSplit);
- s = s.Insert(i+1 ,"_");
- if (useFakeEncryption) {
- s = s.Insert(i,fakeEncryptionChars[UnityEngine.Random.Range(0, fakeEncryptionChars.Length)]);
- s = s.Remove (s.Length - 3, 1);
- }
- if (s.Length == s.Length) {
- //s = s.Insert (s.Length - 1, "\n");
- s = s.Remove (s.Length - 1, 1);
- }
- if (keepLog) {
- uiText.text += s[i];
- } else if (!keepLog) {
- uiText.text = s;
- }
- if (typeSound1) {
- //SoundManager.instance.PlaySingle (typeSound1);
- yield return null;
- }
- if (typeSound1 && typeSound2) {
- //SoundManager.instance.RandomizeSfx (typeSound1, typeSound2);
- yield return 0;
- }
- if (waitForLineBreak && textToPrintSplit[i] == waitForLineBreakChar) {
- yield return new WaitForSeconds (lineBreakDelay);
- }
- yield return new WaitForSeconds (UnityEngine.Random.Range(0.01f,charPrintDelay));
- }
- isPrinting = false;
- yield return null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment