SayAllenthing

Untitled

Oct 2nd, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.74 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using TMPro;
  5.  
  6. public class DialogueWindow : MonoBehaviour
  7. {
  8.     public const float kMaxTextTime = 0.1f;
  9.     public static int TextSpeed = 2;
  10.  
  11.     public TMP_Text Text;
  12.     public string CurrentText;
  13.  
  14.     public GameObject Cursor;
  15.  
  16.     Animator Anim;
  17.  
  18.     protected enum TextStateType
  19.     {
  20.         None,
  21.         Writing,
  22.         Waiting
  23.     }
  24.  
  25.     TextStateType TextState;
  26.  
  27.     private void Start()
  28.     {
  29.         Anim = GetComponent<Animator>();
  30.     }
  31.  
  32.     public void Show(string dialogue)
  33.     {
  34.         Anim.SetBool("Open", true);
  35.  
  36.         CurrentText = dialogue;
  37.     }
  38.  
  39.     public void Close()
  40.     {
  41.         Anim.SetBool("Open", false);
  42.     }
  43.  
  44.     public void OnDialogueOpen()
  45.     {
  46.         StartCoroutine(DisplayText());
  47.     }
  48.  
  49.     public void OnDialogueClosed()
  50.     {
  51.         StopAllCoroutines();
  52.         Text.text = "";
  53.         TextState = TextStateType.None;
  54.  
  55.         Cursor.SetActive(false);
  56.     }
  57.  
  58.     protected IEnumerator DisplayText()
  59.     {
  60.         Text.text = "";
  61.         TextState = TextStateType.Writing;
  62.         Cursor.SetActive(false);
  63.  
  64.         string originalText = CurrentText;
  65.         string displayedText = "";
  66.         int alphaIndex = 0;
  67.  
  68.         foreach (char c in CurrentText.ToCharArray())
  69.         {
  70.             alphaIndex++;
  71.             Text.text = originalText;
  72.             displayedText = Text.text.Insert(alphaIndex, "<color=#00000000>");
  73.             Text.text = displayedText;
  74.            
  75.             yield return new WaitForSecondsRealtime(kMaxTextTime / TextSpeed);
  76.         }
  77.  
  78.         TextState = TextStateType.Waiting;
  79.         //Cursor.SetActive(true);
  80.  
  81.         yield return null;
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment