79 lines
1.9 KiB
C#
79 lines
1.9 KiB
C#
using System.Collections;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class NPCDialogue : MonoBehaviour
|
|
{
|
|
public GameObject dialoguePanel;
|
|
public TextMeshProUGUI dialogueText;
|
|
public string[] dialogue;
|
|
private int index;
|
|
|
|
public GameObject contButton;
|
|
|
|
public float wordSpeed;
|
|
public bool playerIsClose;
|
|
|
|
bool isTyping = false;
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
if(Input.GetKeyDown(KeyCode.E) && playerIsClose && isTyping == false) {
|
|
if (dialoguePanel.activeInHierarchy) {
|
|
// zeroText();
|
|
NextLine();
|
|
} else {
|
|
dialoguePanel.SetActive(true);
|
|
contButton.SetActive(false);
|
|
StartCoroutine(Typing());
|
|
}
|
|
}
|
|
|
|
if(dialogueText.text == dialogue[index]) {
|
|
contButton.SetActive(true);
|
|
isTyping = false;
|
|
}
|
|
}
|
|
|
|
public void zeroText() {
|
|
dialogueText.text = "";
|
|
index = 0;
|
|
isTyping = false;
|
|
dialoguePanel.SetActive(false);
|
|
}
|
|
|
|
IEnumerator Typing() {
|
|
foreach(char letter in dialogue[index].ToCharArray()) {
|
|
dialogueText.text += letter;
|
|
yield return new WaitForSeconds(wordSpeed);
|
|
}
|
|
}
|
|
|
|
public void NextLine () {
|
|
contButton.SetActive(false);
|
|
if (index < dialogue.Length - 1) {
|
|
index++;
|
|
dialogueText.text = "";
|
|
isTyping = true;
|
|
StartCoroutine(Typing());
|
|
} else {
|
|
zeroText();
|
|
}
|
|
}
|
|
|
|
private void OnTriggerEnter2D(Collider2D other) {
|
|
if (other.CompareTag("Player")) {
|
|
playerIsClose = true;
|
|
}
|
|
}
|
|
|
|
private void OnTriggerExit2D(Collider2D other) {
|
|
if (other.CompareTag("Player")) {
|
|
playerIsClose = false;
|
|
zeroText();
|
|
}
|
|
}
|
|
|
|
}
|