using System.Collections; using System.Collections.Generic; using System.Linq; using TMPro; using UnityEngine; namespace Scampz.GameJam { public class DialogueManager : MonoBehaviour { [SerializeField] private TextMeshProUGUI dialogueTextGUI; public float TextSpeed = 0.1f; public Queue sentences; private static DialogueManager _instance; public static DialogueManager Instance { get { if (!_instance) { _instance = new GameObject().AddComponent(); _instance.name = _instance.GetType().ToString(); DontDestroyOnLoad(_instance.gameObject); } return _instance; } } public void Start() { sentences = new Queue(); } public void StartDialogue(Dialogue dialogue) { sentences.Clear(); foreach (var sentence in dialogue.sentences) sentences.Enqueue(sentence); DisplayNextSentence(); } public void DisplayNextSentence() { if (!sentences.Any()) EndDialogue(); var sentence = sentences.Dequeue(); StopAllCoroutines(); StartCoroutine(TypeSentence(sentence)); } private IEnumerator TypeSentence(string sentence) { dialogueTextGUI.text = string.Empty; foreach (var letter in sentence.ToCharArray()) { dialogueTextGUI.text += letter; yield return null; yield return new WaitForSecondsRealtime(TextSpeed); } } private void EndDialogue() { // do nothing currently } } }