Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DialogueBox Start
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class DialogueBox : MonoBehaviour
- {
- [SerializeField] private Text textArea;
- private const int MAX_CHAR = 256;
- private static string[] dialogueGroup;
- private string text;
- private int lineIdx = 0;
- private int idx = 0;
- private bool writingDialogue = false;
- private static bool newDialogue = false;
- /// <summary>
- /// sets the dialogue group and starts the reading
- /// </summary>
- /// <param name="group">new group of dialogue</param>
- public static void SetDialogueGroup(string[] group)
- {
- dialogueGroup = group;
- newDialogue = true; // when this is true the update method will start reading the dialogue
- }
- // Start is called before the first frame update
- void Start()
- {
- // setting this also sets the variables that tells the update function it can start running dialogue
- SetDialogueGroup(new string[] { DialogueParser.PullDialogue(2, "Assets\\Dialogue\\test.txt"), DialogueParser.PullDialogue(3, "Assets\\Dialogue\\test.txt") });
- }
- // used to test if loading dialogue later will trigger it to write
- void LaterInvoke()
- {
- SetDialogueGroup(new string[] { DialogueParser.PullDialogue(2, "Assets\\Dialogue\\test.txt"), DialogueParser.PullDialogue(3, "Assets\\Dialogue\\test.txt") });
- }
- // Update is called once per frame
- void Update()
- {
- // If there is a dialogue group run this
- if (dialogueGroup != null)
- {
- // if the dialogue is new start reading it for the first time
- if (newDialogue)
- {
- StartReadingNewDialogue();
- }
- // handles moving to new dialogue and ending the current dialogue
- DialogueSpaceDown();
- }
- }
- // sets the variabes for reading new dialogue and actually starting the coroutine
- private void StartReadingNewDialogue()
- {
- newDialogue = false;
- writingDialogue = true;
- lineIdx = 0;
- idx = 0;
- text = dialogueGroup[lineIdx];
- StartCoroutine("TypeTextInDialogueBox");
- }
- // handles writing skipping writing and ending the dialogue sequence
- private void DialogueSpaceDown()
- {
- if (Input.GetKeyDown(KeyCode.Space))
- {
- if (writingDialogue)
- {
- textArea.text = text;
- idx = text.Length;
- }
- if (lineIdx < dialogueGroup.Length - 1 && !writingDialogue)
- {
- textArea.text = "";
- lineIdx++;
- text = dialogueGroup[lineIdx];
- idx = 0;
- writingDialogue = true;
- StartCoroutine("TypeTextInDialogueBox");
- }
- if (lineIdx > dialogueGroup.Length - 1 && !writingDialogue)
- {
- textArea.text = "";
- dialogueGroup = null;
- lineIdx = 0;
- idx = 0;
- }
- }
- }
- // coroutine that types out text line by line
- private IEnumerator TypeTextInDialogueBox()
- {
- textArea.text += text[idx++];
- yield return new WaitForSeconds(0.03f);
- if (idx != text.Length)
- {
- StartCoroutine("TypeTextInDialogueBox");
- }
- else
- {
- writingDialogue = false;
- if (lineIdx == dialogueGroup.Length - 1) lineIdx++;
- }
- }
- }
- // Dialogue Parser Start
- using System.Collections;
- using System.Collections.Generic;
- using System.IO;
- using UnityEngine;
- public class DialogueParser : MonoBehaviour
- {
- public static string PullDialogue(int line, string file)
- {
- return File.ReadAllLines(file)[line-1];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement