DialogueManager;cs for Unity


SUBMITTED BY: Taluigi

DATE: Feb. 11, 2020, 9:35 p.m.

FORMAT: C++

SIZE: 1.6 kB

HITS: 1188

  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class DialogueManager : MonoBehaviour {
  6. public Text nameText;
  7. public Text dialogueText;
  8. public CharacterController player;
  9. public Animator animator;
  10. private Queue<string> sentences;
  11. // Use this for initialization
  12. void Start () {
  13. sentences = new Queue<string>();
  14. }
  15. // Update is called once per frame
  16. void Update () {
  17. }
  18. public void StartDialogue(Dialogue dialogue)
  19. {
  20. animator.SetBool("IsOpen", true);
  21. nameText.text = dialogue.name;
  22. sentences.Clear();
  23. foreach(string sentence in dialogue.sentences)
  24. {
  25. sentences.Enqueue(sentence);
  26. }
  27. DisplayNextSentence();
  28. }
  29. public void DisplayNextSentence()
  30. {
  31. if(sentences.Count == 0)
  32. {
  33. EndDialogue();
  34. }
  35. string sentence = sentences.Dequeue();
  36. StopAllCoroutines();
  37. StartCoroutine(TypeSentence(sentence));
  38. }
  39. //Used without update Method
  40. //Yield return null -> says continue if not finished with foreach (while loop)
  41. IEnumerator TypeSentence (string sentence)
  42. {
  43. dialogueText.text = "";
  44. foreach(char letter in sentence.ToCharArray())
  45. {
  46. dialogueText.text += letter;
  47. yield return null;
  48. }
  49. }
  50. void EndDialogue()
  51. {
  52. animator.SetBool("IsOpen", false);
  53. player.freeze = false;
  54. }
  55. }

comments powered by Disqus