using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class DialogueManager : MonoBehaviour { public Text nameText; public Text dialogueText; public CharacterController player; public Animator animator; private Queue sentences; // Use this for initialization void Start () { sentences = new Queue(); } // Update is called once per frame void Update () { } public void StartDialogue(Dialogue dialogue) { animator.SetBool("IsOpen", true); nameText.text = dialogue.name; sentences.Clear(); foreach(string sentence in dialogue.sentences) { sentences.Enqueue(sentence); } DisplayNextSentence(); } public void DisplayNextSentence() { if(sentences.Count == 0) { EndDialogue(); } string sentence = sentences.Dequeue(); StopAllCoroutines(); StartCoroutine(TypeSentence(sentence)); } //Used without update Method //Yield return null -> says continue if not finished with foreach (while loop) IEnumerator TypeSentence (string sentence) { dialogueText.text = ""; foreach(char letter in sentence.ToCharArray()) { dialogueText.text += letter; yield return null; } } void EndDialogue() { animator.SetBool("IsOpen", false); player.freeze = false; } }