SayAllenthing

First Script

Oct 2nd, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 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.     protected enum TextStateType
  15.     {
  16.         None,
  17.         Writing,
  18.         Waiting
  19.     }
  20.  
  21.     TextStateType TextState;
  22.  
  23.     CanvasGroup Group;
  24.  
  25.     private void Start()
  26.     {
  27.         Group = GetComponent<CanvasGroup>();
  28.         Group.alpha = 0;
  29.     }
  30.  
  31.     public void Show(string dialogue)
  32.     {
  33.         CurrentText = dialogue;
  34.         StartCoroutine(DisplayText());
  35.         Group.alpha = 1;
  36.     }
  37.  
  38.     public void Close()
  39.     {
  40.         Group.alpha = 0;
  41.         Text.text = "";
  42.         TextState = TextStateType.None;
  43.     }
  44.  
  45.     protected IEnumerator DisplayText()
  46.     {
  47.         Text.text = "";
  48.         TextState = TextStateType.Writing;
  49.  
  50.         foreach (char c in CurrentText.ToCharArray())
  51.         {
  52.             Text.text += c;
  53.            
  54.             yield return new WaitForSecondsRealtime(kMaxTextTime / TextSpeed);
  55.         }
  56.  
  57.         TextState = TextStateType.Waiting;
  58.  
  59.         yield return null;
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment