91 lines
1.8 KiB
C#
91 lines
1.8 KiB
C#
using System;
|
|
using System.IO;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
|
|
public class DialogueManager : MonoBehaviour
|
|
{
|
|
#region Statication
|
|
|
|
public static DialogueManager instance;
|
|
|
|
private void Awake()
|
|
{
|
|
if (instance != null && instance != this)
|
|
{
|
|
Destroy(gameObject);
|
|
return;
|
|
}
|
|
instance = this;
|
|
}
|
|
|
|
#endregion
|
|
|
|
[Header("Dialogue Script")]
|
|
[SerializeField] private DialogueScript currentDialogueScript;
|
|
private int textProgress;
|
|
public string[] scriptLines;
|
|
[Header("UI")]
|
|
public GameObject uiCanvas;
|
|
public TextMeshProUGUI nameText;
|
|
public TextMeshProUGUI dialogueText;
|
|
|
|
private void Start()
|
|
{
|
|
StartDialogue(currentDialogueScript);
|
|
}
|
|
|
|
public void StartDialogue(DialogueScript scriptToShow)
|
|
{
|
|
uiCanvas.SetActive(true);
|
|
currentDialogueScript = scriptToShow;
|
|
scriptLines = currentDialogueScript.script.text.Split("\n");
|
|
UpdateDialogue();
|
|
}
|
|
|
|
private void ContinueDialogue()
|
|
{
|
|
textProgress++;
|
|
UpdateDialogue();
|
|
}
|
|
|
|
private void UpdateDialogue()
|
|
{
|
|
string[] words = scriptLines[textProgress].Split(" ");
|
|
switch (words[0].ToUpper())
|
|
{
|
|
case "END":
|
|
EndDialogue();
|
|
break;
|
|
case "TEXT":
|
|
foreach (DialogueCharacter character in currentDialogueScript.characters)
|
|
{
|
|
if (string.Equals(character.name, words[1], StringComparison.CurrentCultureIgnoreCase))
|
|
{
|
|
nameText.text = character.name;
|
|
string text = string.Join(" ", words[2..]);
|
|
text = text.Replace("\"", "");
|
|
dialogueText.text = text;
|
|
break;
|
|
}
|
|
}
|
|
break;
|
|
|
|
}
|
|
}
|
|
|
|
public void EndDialogue()
|
|
{
|
|
//TaskManager.instance.AddTask(currentDialogueScript.taskToStart);
|
|
uiCanvas.SetActive(false);
|
|
currentDialogueScript = null;
|
|
scriptLines = null;
|
|
}
|
|
private void Update()
|
|
{
|
|
if (currentDialogueScript && Input.GetMouseButtonDown(0))
|
|
{
|
|
ContinueDialogue();
|
|
}
|
|
}
|
|
}
|