A game about forced loneliness, made by TACStudios
at master 2.4 kB view raw
1using System.Collections; 2using TMPro; 3using UnityEngine; 4using UnityEngine.SceneManagement; 5using UnityEngine.UI; 6 7public class DialogueSystem : MonoBehaviour 8{ 9 [Header("UI Elements")] 10 public TMP_Text dialogueText; 11 public GameObject dialoguePanel; 12 13 [Header("Dialogue Settings")] 14 public string[] predefinedDialogue; 15 public float characterDisplaySpeed = 0.05f; 16 17 private bool isTyping = false; 18 private bool skipTyping = false; 19 private int currentLineIndex = 0; 20 21 22 private void Start() 23 { 24 StartPredefinedDialogue(); 25 } 26 private void Update() 27 { 28 if (Input.GetKeyDown(KeyCode.Space) || Input.GetMouseButtonDown(0)) 29 { 30 if (isTyping) 31 { 32 skipTyping = true; 33 } 34 else 35 { 36 DisplayNextLine(); 37 } 38 } 39 } 40 41 public void StartPredefinedDialogue() 42 { 43 if (predefinedDialogue.Length > 0) 44 { 45 dialoguePanel.SetActive(true); 46 currentLineIndex = 0; 47 StartCoroutine(TypeLine(predefinedDialogue[currentLineIndex])); 48 } 49 } 50 51 public void StartCustomDialogue(string[] customDialogue) 52 { 53 if (customDialogue.Length > 0) 54 { 55 dialoguePanel.SetActive(true); 56 currentLineIndex = 0; 57 predefinedDialogue = customDialogue; 58 StartCoroutine(TypeLine(predefinedDialogue[currentLineIndex])); 59 } 60 } 61 62 private IEnumerator TypeLine(string line) 63 { 64 isTyping = true; 65 dialogueText.text = ""; 66 foreach (char c in line) 67 { 68 if (skipTyping) 69 { 70 dialogueText.text = line; 71 break; 72 } 73 74 dialogueText.text += c; 75 yield return new WaitForSeconds(characterDisplaySpeed); 76 } 77 78 isTyping = false; 79 skipTyping = false; 80 } 81 82 private void DisplayNextLine() 83 { 84 currentLineIndex++; 85 if (currentLineIndex < predefinedDialogue.Length) 86 { 87 StartCoroutine(TypeLine(predefinedDialogue[currentLineIndex])); 88 } 89 else 90 { 91 EndDialogue(); 92 } 93 } 94 95 private void EndDialogue() 96 { 97 dialoguePanel.SetActive(false); 98 dialogueText.text = ""; 99 100 SceneManager.LoadScene("Game"); 101 } 102}