using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro; using UnityEngine.SceneManagement; using System; public class Dialogue : MonoBehaviour { public TextMeshProUGUI textComponent; public string[] lines; //lines to be displayed public float textSpeed; //speed text is written private int index; //current dialogue line displayed private void Start() { textComponent.text = string.Empty; //empty string to initialize StartDialogue(); //run dialogue } void Update() { if(Input.GetMouseButtonDown(0)) //left click to continue to next dialogue line { if (textComponent.text == lines[index]) { NextLine(); } else { StopAllCoroutines(); textComponent.text = lines[index]; } } if(Input.GetKeyDown(KeyCode.E)) //E to return to the menu { SceneManager.LoadScene("Menu"); } else if(Input.GetKeyDown(KeyCode.Q)) //Q to quit the game { Application.Quit(); #if UNITY_EDITOR UnityEditor.EditorApplication.isPlaying = false; #endif } } void StartDialogue() { index = 0; //Initialize the indes to first line StartCoroutine(TypeLine()); //start displaying the text character by character } IEnumerator TypeLine() { foreach (char c in lines[index].ToCharArray()) //each character displayed one by one { textComponent.text += c; yield return new WaitForSeconds(textSpeed); } } void NextLine() { if (index < lines.Length -1) //more lines to display? { index++; //Index to next line incremented textComponent.text = string.Empty; //clear text StartCoroutine(TypeLine()); //start displaying next line } else { SceneManager.LoadScene("Menu"); //load menu if all lines are displayed gameObject.SetActive(false); } } }