martigpg3

DialogueScript

Sep 14th, 2025
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.29 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5.  
  6. public class DialogueManager : MonoBehaviour
  7. {
  8.     int Count = 0;
  9.  
  10.     public GameObject DialogueBox;
  11.     public GameObject textBoxObj;
  12.     public GameObject talkerTextBox;
  13.     AudioSource SFX;
  14.  
  15.     private void Start()
  16.     {
  17.         SFX = this.GetComponent<AudioSource>();
  18.     }
  19.  
  20.     public void StartDialogue(int count, string[] lines, string talker)
  21.     {
  22.         Count = count;
  23.  
  24.         DialogueBox.SetActive(true);
  25.         talkerTextBox.GetComponent<TextMeshProUGUI>().text = talker;
  26.  
  27.         ChangeText(lines);
  28.     }
  29.  
  30.     public void ChangeText(string[] lines)
  31.     {
  32.         if (Count >= lines.Length)
  33.         {
  34.             DialogueBox.SetActive(false);
  35.             return;
  36.         }
  37.        
  38.         textBoxObj.GetComponent<TextMeshProUGUI>().text = lines[Count];
  39.         SFX.Play();
  40.  
  41.         StartCoroutine(WaitForKeyPress(lines));
  42.     }
  43.  
  44.     private IEnumerator WaitForKeyPress(string[] lines)
  45.     {
  46.         bool done = false;
  47.         while (!done)
  48.         {
  49.             if (Input.GetKeyDown(KeyCode.Space))
  50.             {
  51.                 done = true;
  52.             }
  53.             yield return null;
  54.         }
  55.         Count++;
  56.         ChangeText(lines);
  57.     }
  58. }
  59.  
Advertisement
Add Comment
Please, Sign In to add comment