SayAllenthing

Allen Devs - Complete DialogueWindow

Oct 4th, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. //Created by Allen Devs 2020, free to use in your game, don't sweat it, enjoy!
  2.  
  3. using System.Collections;
  4. using UnityEngine;
  5. using TMPro;
  6.  
  7. public class DialogueWindow : MonoBehaviour
  8. {
  9.     const string kAlphaCode = "<color=#00000000>";
  10.     const float kMaxTextTime = 0.1f;
  11.     public static int TextSpeed = 2;
  12.  
  13.     public TMP_Text Text;
  14.     private string CurrentText;
  15.  
  16.     Animator Anim;
  17.  
  18.     //A prompt that appears when the dialogue is done writing (usually in the bottom right).
  19.     public GameObject CursorObject;
  20.  
  21.     //A state that can be used to block or allow player input.
  22.     public enum DialogueState
  23.     {
  24.         None,
  25.         Writing,
  26.         Waiting
  27.     }
  28.     public DialogueState State;
  29.  
  30.     void Start()
  31.     {
  32.         Anim = GetComponent<Animator>();
  33.         if (Anim == null)
  34.         {
  35.             Debug.LogError("No Animator Controller on DialogueWindow: " + gameObject.name);
  36.         }
  37.     }
  38.  
  39.     public void Show(string text)
  40.     {
  41.         Anim?.SetBool("Open", true);
  42.         CurrentText = text;
  43.         CursorObject?.SetActive(false);
  44.     }
  45.  
  46.     public void Close()
  47.     {        
  48.         Anim?.SetBool("Open", false);
  49.     }
  50.  
  51.     public void OnDialogueOpen()
  52.     {
  53.         StartCoroutine(DisplayText());
  54.     }
  55.  
  56.     public void OnDialogueClosed()
  57.     {
  58.         StopAllCoroutines();
  59.         Text.text = "";
  60.  
  61.         State = DialogueState.None;
  62.         CursorObject?.SetActive(false);
  63.     }
  64.  
  65.     private IEnumerator DisplayText()
  66.     {
  67.         if (Text == null)
  68.         {
  69.             Debug.LogError("Text is not linked in DialogueWindow: " + gameObject.name);
  70.             yield return null;
  71.         }
  72.  
  73.         State = DialogueState.Writing;
  74.  
  75.         Text.text = "";
  76.  
  77.         string originalText = CurrentText;
  78.         string displayedText = "";
  79.         int alphaIndex = 0;
  80.  
  81.         foreach(char c in CurrentText.ToCharArray())
  82.         {
  83.             alphaIndex++;
  84.             Text.text = originalText;
  85.             displayedText = Text.text.Insert(alphaIndex, kAlphaCode);
  86.             Text.text = displayedText;
  87.  
  88.             yield return new WaitForSecondsRealtime(kMaxTextTime / TextSpeed);
  89.         }
  90.  
  91.         State = DialogueState.Waiting;
  92.         CursorObject?.SetActive(true);
  93.  
  94.         yield return null;
  95.     }
  96. }
  97.  
Advertisement
Add Comment
Please, Sign In to add comment