using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class DialogueManager : MonoBehaviour { public Text nameText; public Text dialogueText; public Animator animator; private Queue sentences; // Start is called before the first frame update void Start() { sentences = new Queue(); } public void StartDialogue(Dialogue dialogue) { animator.SetBool("IsOpen", true); Debug.Log("Starting conversation with " + dialogue.name); nameText.text = dialogue.name; sentences.Clear(); foreach (string sentence in dialogue.sentences) { sentences.Enqueue(sentence); } DisplayNextSentences(); } public void DisplayNextSentences() { if (sentences.Count == 0) { EndDialogue(); return; } string sentence = sentences.Dequeue(); StopAllCoroutines(); StartCoroutine(TypeSentence(sentence)); } IEnumerator TypeSentence(string sentence) { dialogueText.text = ""; foreach (char letter in sentence.ToCharArray()) { dialogueText.text += letter; yield return null; } } void EndDialogue() { Debug.Log("End of conversation."); animator.SetBool("IsOpen", false); } }