-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDialogueSystem.cs
More file actions
130 lines (104 loc) · 3.35 KB
/
DialogueSystem.cs
File metadata and controls
130 lines (104 loc) · 3.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using TMPro;
public class DialogueSystem : MonoBehaviour
{
public static DialogueSystem Instance { get; private set; }
[Header("UI Components")]
[SerializeField] private GameObject dialoguePanel;
[SerializeField] private TextMeshProUGUI nameText;
[SerializeField] private TextMeshProUGUI dialogueText;
[SerializeField] private Image speakerImage;
[SerializeField] private Button continueButton;
[Header("Typing Settings")]
[SerializeField] private float typingSpeed = 0.05f;
[SerializeField] private AudioClip typingSound;
[SerializeField] private AudioSource audioSource;
private Queue<string> sentences;
private string currentSpeaker;
private Sprite currentSpeakerImage;
private bool isTyping = false;
private Coroutine typingCoroutine;
private void Awake()
{
if (Instance == null)
{
Instance = this;
DontDestroyOnLoad(gameObject);
sentences = new Queue<string>();
}
else
{
Destroy(gameObject);
}
}
private void Start()
{
dialoguePanel.SetActive(false);
if (continueButton != null)
continueButton.onClick.AddListener(DisplayNextSentence);
}
public void StartDialogue(Dialogue dialogue)
{
dialoguePanel.SetActive(true);
sentences.Clear();
foreach (string sentence in dialogue.sentences)
{
sentences.Enqueue(sentence);
}
currentSpeaker = dialogue.speakerName;
currentSpeakerImage = dialogue.speakerImage;
if (nameText != null)
nameText.text = currentSpeaker;
if (speakerImage != null && currentSpeakerImage != null)
speakerImage.sprite = currentSpeakerImage;
DisplayNextSentence();
}
public void DisplayNextSentence()
{
if (isTyping)
{
// If currently typing, display full text immediately
if (typingCoroutine != null)
StopCoroutine(typingCoroutine);
dialogueText.text = dialogueText.text; // Set to full text
isTyping = false;
return;
}
if (sentences.Count == 0)
{
EndDialogue();
return;
}
string sentence = sentences.Dequeue();
if (typingCoroutine != null)
StopCoroutine(typingCoroutine);
typingCoroutine = StartCoroutine(TypeSentence(sentence));
}
private IEnumerator TypeSentence(string sentence)
{
isTyping = true;
dialogueText.text = "";
foreach (char letter in sentence.ToCharArray())
{
dialogueText.text += letter;
if (typingSound != null && audioSource != null)
audioSource.PlayOneShot(typingSound);
yield return new WaitForSeconds(typingSpeed);
}
isTyping = false;
}
private void EndDialogue()
{
dialoguePanel.SetActive(false);
}
}
[System.Serializable]
public class Dialogue
{
public string speakerName;
public Sprite speakerImage;
public string[] sentences;
}