Advertisement
SayAllenthing

Allen Devs - Complete DialogueWindow

Oct 4th, 2020
1,230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 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.  
  44.         if(CursorObject != null)
  45.             CursorObject?.SetActive(false);
  46.     }
  47.  
  48.     public void Close()
  49.     {        
  50.         Anim?.SetBool("Open", false);
  51.     }
  52.  
  53.     public void OnDialogueOpen()
  54.     {
  55.         StartCoroutine(DisplayText());
  56.     }
  57.  
  58.     public void OnDialogueClosed()
  59.     {
  60.         StopAllCoroutines();
  61.         Text.text = "";
  62.  
  63.         State = DialogueState.None;
  64.  
  65.         if (CursorObject != null)
  66.             CursorObject?.SetActive(false);
  67.     }
  68.  
  69.     private IEnumerator DisplayText()
  70.     {
  71.         if (Text == null)
  72.         {
  73.             Debug.LogError("Text is not linked in DialogueWindow: " + gameObject.name);
  74.             yield return null;
  75.         }
  76.  
  77.         State = DialogueState.Writing;
  78.  
  79.         Text.text = "";
  80.  
  81.         string originalText = CurrentText;
  82.         string displayedText = "";
  83.         int alphaIndex = 0;
  84.  
  85.         foreach(char c in CurrentText.ToCharArray())
  86.         {
  87.             alphaIndex++;
  88.             Text.text = originalText;
  89.             displayedText = Text.text.Insert(alphaIndex, kAlphaCode);
  90.             Text.text = displayedText;
  91.  
  92.             yield return new WaitForSecondsRealtime(kMaxTextTime / TextSpeed);
  93.         }
  94.  
  95.         State = DialogueState.Waiting;
  96.  
  97.         if (CursorObject != null)
  98.             CursorObject?.SetActive(true);
  99.  
  100.         yield return null;
  101.     }
  102. }
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement