using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; public class DialogueManager : MonoBehaviour { private Queue sentences; private Dialogue currentDialogue; public TextMeshProUGUI nameText; public TextMeshProUGUI dialogueText; public TextMeshProUGUI itemText; public GameObject pickupHint; public Animator animator; // Start is called once before the first execution of Update after the MonoBehaviour is created void Start() { sentences = new Queue(); } public void StartDialogue(Dialogue dialogue) { animator.SetBool("IsOpen", true); sentences.Clear(); currentDialogue = dialogue; nameText.text = dialogue.charName; foreach (string sentence in dialogue.sentences) { sentences.Enqueue(sentence); } currentDialogue.isActive = true; DisplayNextSentence(); } public void DisplayNextSentence() { if (sentences.Count == 0) { EndDialogue(); return; } string sentence = sentences.Dequeue(); StopAllCoroutines(); StartCoroutine(TypeSentence(sentence)); } public void EndDialogue() { animator.SetBool("IsOpen", false); if (currentDialogue != null) { currentDialogue.isActive = false; } } IEnumerator TypeSentence(string sentence) { dialogueText.text = ""; foreach (char c in sentence.ToCharArray()) { dialogueText.text += c; yield return new WaitForSeconds(0.01f); } } public void ShowItemText(string itemName) { itemText.text = itemName; pickupHint.SetActive(true); } public void HideItemText() { itemText.text = ""; pickupHint.SetActive(false); } }