AR_Celement/Assets/Scripts/DialogueScript.cs
2023-01-20 12:32:40 +01:00

94 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DialogueScript : MonoBehaviour
{
public Text textComponent;
public string[] lines;
public float textSpeed;
private int index;
public List<int> images;
private GameObject image;
private GameObject textImage;
public GameObject arrow;
public List<GameObject> imageAndTexts;
private int indexList = 0;
private void OnEnable()
{
textComponent.text = string.Empty;
StartDialogue();
}
// Update is called once per frame
void Update()
{
if(images.Contains(index))
{
SetImageStep();
}
if (Input.GetMouseButtonDown(0))
{
if(images.Contains(index - 1)){
DisableImageStep();
}
if (textComponent.text == lines[index])
{
NextLine();
}
else
{
StopAllCoroutines();
textComponent.text = lines[index];
}
}
}
public void StartDialogue()
{
index = 0;
StartCoroutine(TypeLine());
}
IEnumerator TypeLine()
{
foreach (char c in lines[index].ToCharArray())
{
textComponent.text += c;
yield return new WaitForSeconds(textSpeed);
}
}
public void NextLine()
{
if (index < lines.Length - 1)
{
index++;
textComponent.text = string.Empty;
StartCoroutine(TypeLine());
}
else
{
gameObject.SetActive(false);
arrow.SetActive(true);
}
}
private void SetImageStep(){
textImage = imageAndTexts[indexList];
indexList ++;
image = imageAndTexts[indexList];
indexList ++;
image.SetActive(true);
textImage.SetActive(true);
index ++;
}
private void DisableImageStep(){
image.SetActive(false);
textImage.SetActive(false);
}
}