Advertisement
BenTibnam

DialogueBox and DialogueParser

Feb 5th, 2021
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.92 KB | None | 0 0
  1. // DialogueBox Start
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.UI;
  6.  
  7. public class DialogueBox : MonoBehaviour
  8. {
  9.     [SerializeField] private Text textArea;
  10.     private const int MAX_CHAR = 256;
  11.  
  12.     private static string[] dialogueGroup;
  13.     private string text;
  14.     private int lineIdx = 0;
  15.     private int idx = 0;
  16.     private bool writingDialogue = false;
  17.     private static bool newDialogue = false;
  18.  
  19.     /// <summary>
  20.     /// sets the dialogue group and starts the reading
  21.     /// </summary>
  22.     /// <param name="group">new group of dialogue</param>
  23.     public static void SetDialogueGroup(string[] group)
  24.     {
  25.         dialogueGroup = group;
  26.         newDialogue = true; // when this is true the update method will start reading the dialogue
  27.     }
  28.    
  29.     // Start is called before the first frame update
  30.     void Start()
  31.     {
  32.         // setting this also sets the variables that tells the update function it can start running dialogue
  33.         SetDialogueGroup(new string[] { DialogueParser.PullDialogue(2, "Assets\\Dialogue\\test.txt"), DialogueParser.PullDialogue(3, "Assets\\Dialogue\\test.txt") });
  34.        
  35.     }
  36.  
  37.     // used to test if loading dialogue later will trigger it to write
  38.     void LaterInvoke()
  39.     {
  40.         SetDialogueGroup(new string[] { DialogueParser.PullDialogue(2, "Assets\\Dialogue\\test.txt"), DialogueParser.PullDialogue(3, "Assets\\Dialogue\\test.txt") });
  41.     }
  42.  
  43.     // Update is called once per frame
  44.     void Update()
  45.     {
  46.         // If there is a dialogue group run this
  47.         if (dialogueGroup != null)
  48.         {
  49.             // if the dialogue is new start reading it for the first time
  50.             if (newDialogue)
  51.             {
  52.                 StartReadingNewDialogue();
  53.             }
  54.  
  55.             // handles moving to new dialogue and ending the current dialogue
  56.             DialogueSpaceDown();
  57.         }
  58.  
  59.     }
  60.  
  61.     // sets the variabes for reading new dialogue and actually starting the coroutine
  62.     private void StartReadingNewDialogue()
  63.     {
  64.         newDialogue = false;
  65.         writingDialogue = true;
  66.         lineIdx = 0;
  67.         idx = 0;
  68.         text = dialogueGroup[lineIdx];
  69.         StartCoroutine("TypeTextInDialogueBox");
  70.     }
  71.  
  72.     // handles writing skipping writing and ending the dialogue sequence
  73.     private void DialogueSpaceDown()
  74.     {
  75.         if (Input.GetKeyDown(KeyCode.Space))
  76.         {
  77.  
  78.             if (writingDialogue)
  79.             {
  80.                 textArea.text = text;
  81.                 idx = text.Length;
  82.             }
  83.  
  84.             if (lineIdx < dialogueGroup.Length - 1 && !writingDialogue)
  85.             {
  86.                 textArea.text = "";
  87.                 lineIdx++;
  88.                 text = dialogueGroup[lineIdx];
  89.                 idx = 0;
  90.                 writingDialogue = true;
  91.                 StartCoroutine("TypeTextInDialogueBox");
  92.             }
  93.  
  94.             if (lineIdx > dialogueGroup.Length - 1 && !writingDialogue)
  95.             {
  96.                 textArea.text = "";
  97.                 dialogueGroup = null;
  98.                 lineIdx = 0;
  99.                 idx = 0;
  100.             }
  101.         }
  102.     }
  103.  
  104.     // coroutine that types out text line by line
  105.     private IEnumerator TypeTextInDialogueBox()  
  106.     {
  107.         textArea.text += text[idx++];
  108.        
  109.         yield return new WaitForSeconds(0.03f);
  110.  
  111.         if (idx != text.Length)
  112.         {
  113.             StartCoroutine("TypeTextInDialogueBox");
  114.         }
  115.         else
  116.         {
  117.             writingDialogue = false;
  118.             if (lineIdx == dialogueGroup.Length - 1) lineIdx++;
  119.         }
  120.        
  121.     }
  122. }
  123.  
  124. // Dialogue Parser Start
  125.  
  126. using System.Collections;
  127. using System.Collections.Generic;
  128. using System.IO;
  129. using UnityEngine;
  130.  
  131. public class DialogueParser : MonoBehaviour
  132. {
  133.     public static string PullDialogue(int line, string file)
  134.     {
  135.         return File.ReadAllLines(file)[line-1];
  136.     }
  137. }
  138.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement