maxhacker11

DialogueManager.cs

Nov 18th, 2023
7,319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.54 KB | Source Code | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine.UI;
  4. using UnityEngine;
  5. using TMPro;
  6.  
  7. public class DialogueManager : MonoBehaviour
  8. {
  9.     public static DialogueManager Instance;
  10.  
  11.     public Image characterIcon;
  12.     public TextMeshProUGUI characterName;
  13.     public TextMeshProUGUI dialogueArea;
  14.  
  15.     private Queue<DialogueLine> lines;
  16.    
  17.     public bool isDialogueActive = false;
  18.  
  19.     public float typingSpeed = 0.2f;
  20.  
  21.     public Animator animator;
  22.  
  23.     private void Awake()
  24.     {
  25.         if (Instance == null)
  26.             Instance = this;
  27.  
  28.         lines = new Queue<DialogueLine>();
  29.     }
  30.  
  31.     public void StartDialogue(Dialogue dialogue)
  32.     {
  33.         isDialogueActive = true;
  34.  
  35.         animator.Play("show");
  36.  
  37.         lines.Clear();
  38.  
  39.         foreach (DialogueLine dialogueLine in dialogue.dialogueLines)
  40.         {
  41.             lines.Enqueue(dialogueLine);
  42.         }
  43.  
  44.         DisplayNextDialogueLine();
  45.     }
  46.  
  47.     public void DisplayNextDialogueLine()
  48.     {
  49.         if (lines.Count == 0)
  50.         {
  51.             EndDialogue();
  52.             return;
  53.         }
  54.  
  55.         DialogueLine currentLine = lines.Dequeue();
  56.  
  57.         characterIcon.sprite = currentLine.character.icon;
  58.         characterName.text = currentLine.character.name;
  59.  
  60.         StopAllCoroutines();
  61.  
  62.         StartCoroutine(TypeSentence(currentLine));
  63.     }
  64.  
  65.     IEnumerator TypeSentence(DialogueLine dialogueLine)
  66.     {
  67.         dialogueArea.text = "";
  68.         foreach (char letter in dialogueLine.line.ToCharArray())
  69.         {
  70.             dialogueArea.text += letter;
  71.             yield return new WaitForSeconds(typingSpeed);
  72.         }
  73.     }
  74.  
  75.     void EndDialogue()
  76.     {
  77.         isDialogueActive = false;
  78.         animator.Play("hide");
  79.     }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment